|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am trying to validate that a 'First date' entered in a form comes before a 'last date,' and that both dates entered come before today's date. Respondents are allowed to leave the field blank (coded as 99, below) or answer "don't know" (coded as 88, below). Using the code below, I was able to confirm that the first entered date comes before the second entered date, but I am not able to confirm that either date come before today's date. Any suggestions would be greatly appreciated.
Thanks in advance, Atar ------------------------------------ <script language="JavaScript"> <!-- Begin function y2k(number) { return (number < 1000) ? number + 1900 : number;} var today=new Date(); var now=Date.UTC(y2k(today.getYear()),today.getMonth(),today.getDate(),0,0,0); function valid_date(firstdiary) { var fintday1 = firstdiary.fintday1.options[firstdiary.fintday1.selectedIndex].value; var fintmo1 = firstdiary.fintmo1.options[firstdiary.fintmo1.selectedIndex].value; var fintyr1 = firstdiary.fintyr1.options[firstdiary.fintyr1.selectedIndex].value; var lintday1 = firstdiary.lintday1.options[firstdiary.lintday1.selectedIndex].value; var lintmo1 = firstdiary.lintmo1.options[firstdiary.lintmo1.selectedIndex].value; var lintyr1 = firstdiary.lintyr1.options[firstdiary.lintyr1.selectedIndex].value; startdate = new Date(fintyr1-0,fintmo1-1,fintday1-0); enddate = new Date(lintyr1-0,lintmo1-1,lintday1-0); if ((fintday1!=99) && (fintday1!=88) && (fintmo1!=99) && (fintmo1!=88) && (fintyr1!=99) && (fintyr1!=88)) { starttime=Date.UTC(y2k(startdate.getYear()),startdate.getMonth(),startdate.getDate(),0,0,0); } else (starttime = 'missing'); if ((lintday1!=99) && (lintday1!=88) && (lintmo1!=99) && (lintmo1!=88) && (lintyr1!=99) && (lintyr1!=88)) { endtime = Date.UTC(y2k(enddate.getYear()),enddate.getMonth(),enddate.getDate(),0,0,0); } else (endtime = 'missing'); if ((starttime > now) && (starttime!='missing')){ alert('First date cannot be in the future'); return false; } // the alert above doesn't work else if ((starttime > endtime) && (endtime!='missing') && (starttime!='missing')){ alert('First date cannot be after last date'); return false; } // the alert above does work else if ((endtime > now) && (endtime!='missing')){ alert('Last date cannot be in the future'); return false; } // the alert above doesn't work else{ // valid } return true; } //--> </script> |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > Validate that a date comes before today's date |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|