I have two date field viz from-date and to-date
and i want to validate that from-date should be less than or equal to to-date
using javascript.
Note: date field should be in yy-mm-dd format
How is that possible ?
Please help me..
Thanks in advance..
| SitePoint Sponsor |
I have two date field viz from-date and to-date
and i want to validate that from-date should be less than or equal to to-date
using javascript.
Note: date field should be in yy-mm-dd format
How is that possible ?
Please help me..
Thanks in advance..




if the Date fields validate subtract their Date objects-
if(new Date(from_date)-new Date(to_date)>0){
throw new Error('Time travel is not allowed');
}
else// to_date is after from_date
Last edited by mrhoo; Feb 22, 2007 at 21:30.





mrhoo's solution will not work for some users. Try this instead:
Code:var from = "08-11-10"; var to = "07-11-10"; alert(isFirstEarlier(from, to)); function isFirstEarlier(d1, d2) { var d1pieces = d1.split("-"); var d2pieces = d2.split("-"); var date1 = new Date(d1pieces[0], d1pieces[1], d1pieces[2]); var date2 = new Date(d2pieces[0], d2pieces[1], d2pieces[2]); return date1 <= date2; //compares milliseconds since the epoch }
Bookmarks