logo

Response

« Return to the main article

You are viewing this page out of context. To see it in the context it is intended please click here.

About This Page

Reply posted by Jake Howlett on Fri 9 Aug 2002 in response to Form Validator R2.0

Re: Date Validation?

Mr Paynter,


I take it this means you have a job then?


You won't be able to do it this way as, as it stands, you are trying to compare
two *strings*. You need to convert them both to JS time/date objects first.
Here's a function that should work:


[<code>]function checkDateFollows( dp1, up1 ){


dp1 = frm.FieldA.value;
up1 = frm.FieldB.value;


ds1 = dp1.split('/');
ds2 = up1.split('/');


d1 = new Date();
d2 = new Date();

d1.setFullYear(parseInt(ds1[2],10));
d1.setMonth(parseInt(ds1[1],10)-1);
d1.setDate(parseInt(ds1[0],10));
d1.setHours(0);
d1.setMinutes(0);


d2.setFullYear(parseInt(ds2[2],10));
d2.setMonth(parseInt(ds2[1],10)-1);
d2.setDate(parseInt(ds2[0],10));
d2.setHours(0);
d2.setMinutes(0);


return ( d1.getTime() <= d2.getTime() );
}[</code>]


Which you would call from anywhere in your script like so:


[<code>]if ( checkDateFollows( f.DateA.value, f.DateB.value) ) {
alert("The Date B can't be before Date A");
f.DateB.focus();
return false
}[</code>]


See you soon,
Jake