Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object expected

Status
Not open for further replies.

ericaalicen

Programmer
Dec 20, 2001
53
US
I'm still stuck on adding 7 days to a date. I think the problem is in the way I'm calling the function or passing the value? I'm getting an object expected error. I do have some Cold Fusion in here as well, that appears to be fine.

<cfparam name=&quot;start&quot; default=&quot;#Now()#&quot;>
<cfparam name=&quot;end&quot; default=&quot;#Evaluate(start + 7)#&quot;>

<form name=&quot;form&quot; method=&quot;post&quot; action=&quot;avail.cfm?add=1&quot;>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;rental_id&quot; value=&quot;#id#&quot;>
Start: <input type=&quot;text&quot; name=&quot;start&quot; size=&quot;10&quot; value=&quot;#DateFormat(start, 'mm/dd/yyyy')#&quot; onKeyUp=&quot;add(form);&quot;>&nbsp;&nbsp;&nbsp;
End: <input type=&quot;text&quot; name=&quot;end&quot; size=&quot;10&quot; value=&quot;#DateFormat(end, 'mm/dd/yyyy')#&quot;>&nbsp;&nbsp;&nbsp;
Price: <input type=&quot;text&quot; name=&quot;price&quot; size=&quot;10&quot;>&nbsp;&nbsp;&nbsp;
<input type=&quot;submit&quot; value=&quot;Add&quot;>
</cfoutput>
</form>

<script>
function add(form) {
var start = document.form.start.value;
if (start != '') {
var x = setDate(getDate(start) + 7);
var y = setYear(getYear(start));
var m = setMonth(getMonth(start));
var myDate = new Date(y, m, x);
form.end.value = myDate;
}
}
</script>
 
first don't use form as a parameter name. use something like frm or formObj
form is a restricted word

try using a onBlur instead of a onkeyup or even a onChange. ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
I changed the name of the form to frm and tried passing that which didn't work. I've tried it without explicitly passing a value. How to call the function and what I should be passing I don't really understand. I made the event handler onChange=&quot;add();&quot;. Should I be passing a value like onChange=add(start);&quot; or should it be onChange=&quot;return add();&quot;. This last one doesn't quite make sense since I'm not actually returning a value in the function. I've tried all of these and I still get the object expected error. Or maybe this isn't even where the problem is.

<form name=&quot;frm&quot; method=&quot;post&quot; action=&quot;avail.cfm?add=1&quot;>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;rental_id&quot; value=&quot;#id#&quot;>
Start: <input type=&quot;text&quot; name=&quot;start&quot; size=&quot;10&quot; value=&quot;#DateFormat(start, 'mm/dd/yyyy')#&quot; onChange=&quot;add();&quot;>&nbsp;&nbsp;&nbsp;
End: <input type=&quot;text&quot; name=&quot;end&quot; size=&quot;10&quot; value=&quot;#DateFormat(end, 'mm/dd/yyyy')#&quot;>&nbsp;&nbsp;&nbsp;
Price: <input type=&quot;text&quot; name=&quot;price&quot; size=&quot;10&quot;>&nbsp;&nbsp;&nbsp;
<input type=&quot;submit&quot; value=&quot;Add&quot;>
</cfoutput>
</form>

<script>
function add() {
var start = document.frm.start.value;
if (start != '') {
var x = setDate(getDate(start) + 7);
var y = setYear(getYear(start));
var m = setMonth(getMonth(start));
var myDate = new Date(y, m, x);
frm.end.value = myDate;
}
}
</script>
 
is it formatted as you have it there. the script below the html tags.
if so the script needs to be above it in the head ---------------------------------------
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }
---------------------------------------
for the best results to your questions: FAQ333-2924

 
I did have it structured with the script below the form. I've now moved it to the head but I'm still getting the object expected error.
 
Maybe it's because you're not defining start as a date type in the function. Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseIntx.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
I'm not completely sure what you mean by defining start as a date type. I've made it so that when I create the var start in the script that it's as a new date, but it's still not working.

This is the script that's in the head.

<script>
function add() {
var start = new Date(document.frm.start.value);
if (start != '') {
var x = setDate(getDate(start) + 7);
var y = setYear(getYear(start));
var m = setMonth(getMonth(start));
var myDate = new Date(y, m, x);
frm.end.value = myDate;
}
}
</script>

And this is the form.

<form name=&quot;frm&quot; method=&quot;post&quot; action=&quot;avail.cfm?add=1&quot;>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;rental_id&quot; value=&quot;#id#&quot;>
Start: <input type=&quot;text&quot; name=&quot;start&quot; size=&quot;10&quot; value=&quot;#DateFormat(start, 'mm/dd/yyyy')#&quot; onChange=&quot;return add();&quot;>&nbsp;&nbsp;&nbsp;
End: <input type=&quot;text&quot; name=&quot;end&quot; size=&quot;10&quot; value=&quot;#DateFormat(end, 'mm/dd/yyyy')#&quot;>&nbsp;&nbsp;&nbsp;
Price: <input type=&quot;text&quot; name=&quot;price&quot; size=&quot;10&quot;>&nbsp;&nbsp;&nbsp;
<input type=&quot;submit&quot; value=&quot;Add&quot;>
</cfoutput>
</form>

I'm still getting the object expected error.
 
That is what I meant. I think the command syntax may be wrong. Try this rewritten function:
Code:
function add() {
  var start = new Date(document.frm.start.value);
  if (start != '') {
    var x = start.getDate() + 7;
    var y = start.getYear();
    var m = start.getMonth();
    var myDate = new Date(y, m, x);
    document.frm.end.value = myDate;
  }
}

As far as I could make out, if you're assigning y, m & x to myDate then you don't need SetDate and it was the getDate syntax that was wrong.
Let me know.

Terry Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseIntx.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
HellTel

This worked great. But now I need to format the date to mm/dd/yyyy format. I tried incorporating a script I found on the web, but it doesn't make much sense to me. The field displays NaN. Is there any easier way to format a date? Does it need to be a string to be in this format? Thanks for the help.

<script>
function add() {
var start = new Date(document.frm.start.value);
if (start != '') {
var x = start.getDate() + 7;
if (x<10)
x=&quot;0&quot;+x
var y= start.getYear();
var m = start.getMonth()+1;
if (m<10)
m=&quot;0&quot;+m
var myDate = &quot;+m+&quot;/&quot;+x+&quot;/&quot;+y+&quot;;
document.frm.end.value = myDate;
}
}
</script>
 
I think you have your quotes the wrong way round in this line:
Code:
var myDate = &quot;+m+&quot;/&quot;+x+&quot;/&quot;+y+&quot;;

Try this:
Code:
var myDate = m+&quot;/&quot;+x+&quot;/&quot;+y

Basically it probably thought your +m+ etc were strings instead of variables and your / were divide by signs.

Let me know if that works. Hope I helped / Thanks for helping[i]
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseIntx.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}[/i]
 
This worked great. Now I've noticed a problem that I don't believe will be easily resolved. This can't handle a date spread that begins at the end of the month and ends in another month. ie. If you enter 01/26/2003 the ending date is 01/33/2003. Any suggestions? Thanks.
 
Trickier, but distinctly possible. Try this rewritten function:
Code:
function add() {
	var daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var start = new Date(document.frm.start.value);	
  if (start != '') {
    var y= start.getYear();
		// check for leap year (see if year divided by four leaves a remainder). If it is a leap year, add one day to February
		var remainder = y % 4;
		if (remainder == 0) {
			daysInMonth[1]=29;
		}
		var m = start.getMonth();
		var x = start.getDate()+7;
// check for roll over into next month, and then check that for roll into next year.		
		if (x > daysInMonth[m]){
			x = x - daysInMonth[m];
			m++;
			if (m > 11){
				m=0;
				y++;
			}
		}
		// increment month to real month, not &quot;Array&quot; month
		m++;		
		if (x<10)
		x=&quot;0&quot;+x
		if (m<10)
		m=&quot;0&quot;+m
		var myDate = m+&quot;/&quot;+x+&quot;/&quot;+y;
		document.frm.end.value = myDate;
  }
}
Hope I helped / Thanks for helping
if ((Math.abs(x)<10&&Math.abs(y)<10) &&(((parseIntx.toString()+y.toString())-x-y)%9)!=0)){alert(&quot;I'm a monkey's uncle&quot;);}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top