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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range Comparison Validation

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
0
0
US
Hi,

I have a date range form that's pre-populated (ok, hard-coded for now) with today's date.

I want to restrict the user to only using one date. I don't care what date they enter in the 1st MM/DD/YYYY section, but the 2nd MM/DD/YYYY date must be the same as the first.

Preferably, when the user clicks the Submit I'd like the following:

* Compare the first set of dates to the second set. If
they're not the same, fill in the last set of dates with values from the first (this is an extra perk).
* Alert the user and not submit the page.

I created a form and some JS and found a few examples of this forum, but I'm receiving a JS error on my onclick attribute. It doesn't like that line and says an object was expected.

Any help/insight you can provide is greatly appreciated.

My current code follows:


<html>
<head>
<title>
<script language=&quot;Javascript&quot;>
<!---
function checkDate(smonth,sday,syear,emonth,eday,eyear)
{
var date=smonth+sday+syear;
var old=emonth+eday+eyear;
var f = new Date(date);
var n = new Date(old);
if (f != n)
{ return true }
else
{ alert(&quot;You cannot enter more than one day's date for this particular view.&quot;);
return false;
}

}

//--->
</script>
</title>
</head>

<body>

<table cellpadding=0 cellspacing=0 width=100% colspan=2 border=0>
<tr>
<td><b><p>Please enter a date range and click on the report you would like to view.</b></p></td>
</tr>
</table>

<form name=&quot;vaform&quot; action=&quot;form2.cfm&quot; method=&quot;post&quot;>
<TABLE width=60% cellpadding=0 cellspacing=0 border=0>
<TR>
<TD><input type=&quot;text&quot; name=&quot;startmonth&quot; value=&quot;02&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;startday&quot; value=&quot;27&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;startyear&quot; value=&quot;2001&quot; maxlength=4 size=5></TD>
<TD>TO </TD>
<TD><input type=&quot;text&quot; name=&quot;endmonth&quot; value=&quot;02&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;endday&quot; value=&quot;27&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;endyear&quot; value=&quot;2001&quot; maxlength=4 size=5></TD>
<TD>  </TD>
<input type=&quot;hidden&quot; name=&quot;sdate&quot; value=&quot;document.vaform.startmonth.value/document.vaform.startday.value/document.vaform.startyear.value&quot;>
<input type=&quot;hidden&quot; name=&quot;edate&quot; value=&quot;document.vaform.endmonth.value/document.vaform.endday.value/document.vaform.endyear.value&quot;>
<TD><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot; onClick=&quot;return checkDate('document.vaform.startmonth.value','document.vaform.startday.value','document.vaform.startyear.value','document.vaform.endmonth.value','document.vaform.endday.value','document.vaform.endyear.value')&quot;></TD>
<TD><a href=&quot;./homepage.cfm&quot;>Return to Homepage</a></TD>

</TR>

<TR>
<TD><font size=-3>MM</font></TD>
<TD><font size=-3>DD</font></TD>
<TD><font size=-3>YYYY</font></TD>
<TD> </TD>
<TD><font size=-3>MM</font></TD>
<TD><font size=-3>DD</font></TD>
<TD><font size=-3>YYYY</font></TD>
<TD> </TD>
</TR>
</TABLE>
</form>
</body>
</html>


Thanks,
scripter73


Change Your Thinking, Change Your Life.
 
Hi,
I think that the catch is in lines
var date=smonth+sday+syear;
var old=emonth+eday+eyear;
var f = new Date(date);
var n = new Date(old);

in your code.
I've tried it with this code and it wokrs fine
<script language=&quot;JavaScript&quot;>
var d1 = new Date(01/11/2002)
var d2 = new Date(01/11/2002)
alert(&quot;Into Script&quot;)
if (d1 - d2 != 0)
alert(&quot;Not right&quot;)
else
alert(&quot;Right&quot;)
</script>

Should solve your problem.
 
Hi,
I forgot to inform that the problem will be in
var old=emonth+eday+eyear
it should be
var old=emonth + &quot;/&quot; +eday + &quot;/&quot; +eyear
This may solve it. !!
 
Thanks kadirpatel,

I understand what you mean by adding the &quot;/&quot; to the javascript.

However, I did that, and I also changed where the call to checkDate() occurs. I put it in an onsubmit attribute in the form tag. But Javascript is still complaining about the onsubmit=&quot;return checkDate(.....);&quot;. It keeps saying Object Expected. I can't make it out.

Could you paste the following into a page and see what you get? I just don't see any syntax errors.

Thanks again for all of your help.



<html>
<head>
<title>
<script language=&quot;Javascript&quot;>
<!---
function checkDate(smonth,sday,syear,emonth,eday,eyear)
{
var date=smonth + &quot;/&quot; + sday + &quot;/&quot; + syear;
var old=emonth + &quot;/&quot; + eday + &quot;/&quot; + eyear;
var f = new Date(date);
var n = new Date(old);
if (f - n != 0)
{ alert(&quot;You cannot enter more than one day's date for this particular view.&quot;);
return false;
}
else
{ return true; }

}

//--->
</script>

</title>
</head>

<body>

<table cellpadding=0 cellspacing=0 width=100% colspan=2 border=0>
<tr>
<td><b><p>Please enter a date range and click on the report you would like to view.</b></p></td>
</tr>
</table>

<form name=&quot;vaform&quot; action=&quot;form2.html&quot; method=&quot;post&quot; onSubmit=&quot;return checkDate('document.vaform.startmonth.value','document.vaform.startday.value','document.vaform.startyear.value','document.vaform.endmonth.value','document.vaform.endday.value','document.vaform.endyear.value')&quot;>
<TABLE width=60% cellpadding=0 cellspacing=0 border=0>
<TR>
<TD><input type=&quot;text&quot; name=&quot;startmonth&quot; value=&quot;02&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;startday&quot; value=&quot;27&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;startyear&quot; value=&quot;2001&quot; maxlength=4 size=5></TD>
<TD>TO </TD>
<TD><input type=&quot;text&quot; name=&quot;endmonth&quot; value=&quot;02&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;endday&quot; value=&quot;27&quot; maxlength=2 size=3>/</TD>
<TD><input type=&quot;text&quot; name=&quot;endyear&quot; value=&quot;2001&quot; maxlength=4 size=5></TD>
<TD>  </TD>
<TD><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;></TD>
<TD><a href=&quot;./homepage.cfm&quot;>Return to Homepage</a></TD>

</TR>

<TR>
<TD><font size=-3>MM</font></TD>
<TD><font size=-3>DD</font></TD>
<TD><font size=-3>YYYY</font></TD>
<TD> </TD>
<TD><font size=-3>MM</font></TD>
<TD><font size=-3>DD</font></TD>
<TD><font size=-3>YYYY</font></TD>
<TD> </TD>
</TR>
</TABLE>
</form>
</body>
</html>



Thanks again,
scripter73

Change Your Thinking, Change Your Life.
 
Hey Kadirpatel,

DUH!!!!! I found out what I was doing wrong. The <script></script> was in the title. Don't you hate mistakes like that?

Anyway, the code works like a charm now. I decided not to use the Date() function. I really only need the string version.

I'll post it here in case it helps anyone else.

Thanks again,
scripter73



<script language=&quot;Javascript&quot;>
<!---
function checkDate(smonth,sday,syear,emonth,eday,eyear)
{
var date=smonth + &quot;/&quot; + sday + &quot;/&quot; + syear;
var old=emonth + &quot;/&quot; + eday + &quot;/&quot; + eyear;

var f = date;
var n = old;

//alert(&quot;1st date is &quot; + f);
//alert(&quot;2nd date is &quot; + n);

if (f != n)
{ alert(&quot;You cannot enter more than one day's date for this particular view.&quot;);
return false;
}
else
{ return true; }

}

//--->
</script>


Change Your Thinking, Change Your Life.
 
Hey,
Thats great even i could not make it out.
But its good that you have solved your problem.
Hmmm!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top