You are here: irt.org | FAQ | JavaScript | Date | Q548 [ previous next ]
Try:
<script language="JavaScript"><!-- function padout(number) { return (number < 10) ? '0' + number : number; } function y2k(number) { return (number < 1000) ? number + 1900 : number; } function validate(what) { var startday = what.startday.options[what.startday.selectedIndex].value; var startmonth = what.startmonth.options[what.startmonth.selectedIndex].value; var startyear = what.startyear.options[what.startyear.selectedIndex].text; var endday = what.endday.options[what.endday.selectedIndex].value; var endmonth = what.endmonth.options[what.endmonth.selectedIndex].value; var endyear = what.endyear.options[what.endyear.selectedIndex].text; unvalidstartdate = startday + '/' + startmonth + '/' + startyear; unvalidenddate = endday + '/' + endmonth + '/' + endyear; var startdate = new Date(startyear-0,startmonth-1,startday-0); var enddate = new Date(endyear-0,endmonth-1,endday-0); var validstartdate = padout(startdate.getDate()) + '/' + padout(startdate.getMonth()+1) + '/' + y2k(startdate.getYear()) var validenddate = padout(enddate.getDate()) + '/' + padout(enddate.getMonth()+1) + '/' + y2k(enddate.getYear()) if (unvalidstartdate != validstartdate) { alert('Start Date: ' + what.startday.options[what.startday.selectedIndex].text + ' ' + what.startmonth.options[what.startmonth.selectedIndex].text + ' ' + what.startyear.options[what.startyear.selectedIndex].text + ' is invalid'); return false; } if (unvalidenddate != validenddate) { alert('End Date: ' + what.endday.options[what.endday.selectedIndex].text + ' ' + what.endmonth.options[what.endmonth.selectedIndex].text + ' ' + what.endyear.options[what.endyear.selectedIndex].text + ' is invalid'); return false; } starttime = Date.UTC(y2k(startdate.getYear()),startdate.getMonth(),startdate.getDate(),0,0,0); endtime = Date.UTC(y2k(enddate.getYear()),enddate.getMonth(),enddate.getDate(),0,0,0); if (starttime < endtime) { // valid } else { alert('Start Date is not less than End Date'); return false } currentdate = new Date(); currenttime = Date.UTC(y2k(currentdate.getYear()),currentdate.getMonth(),currentdate.getDate(),0,0,0); if (endtime < currenttime) { // valid } else { alert('End Date is not less than todays date'); return false; } what.startdate.value = validstartdate; what.enddate.value = validenddate; return true; } //--></script> <form> Start Date: <select name="startday"> <option value="01">1st <option value="02">2nd <option value="03">3rd <option value="04">4th <option value="05">5th <option value="06">6th <option value="07">7th <option value="08">8th <option value="09">9th <option value="10">10th <option value="11">11th <option value="12">12th <option value="13">13th <option value="14">14th <option value="15">15th <option value="16">16th <option value="17">17th <option value="18">18th <option value="19">19th <option value="20">20th <option value="21">21st <option value="22">22nd <option value="23">23rd <option value="24">24th <option value="25">25th <option value="26">26th <option value="27">27th <option value="28">28th <option value="29">29th <option value="30">30th <option value="31">31st </select> <select name="startmonth"> <option value="01">january <option value="02">february <option value="03">march <option value="04">april <option value="05">may <option value="06">june <option value="07">july <option value="08">august <option value="09">september <option value="10">october <option value="11">november <option value="12">december </select> <select name="startyear"> <option>1990 <option>1991 <option>1992 <option>1993 <option>1994 <option>1995 <option>1996 <option>1997 <option>1998 <option>1999 <option>2000 <option>2001 <option>2002 <option>2003 <option>2004 <option>2005 <option>2006 <option>2007 <option>2008 <option>2009 </select> <p> End Date: <select name="endday"> <option value="01">1st <option value="02">2nd <option value="03">3rd <option value="04">4th <option value="05">5th <option value="06">6th <option value="07">7th <option value="08">8th <option value="09">9th <option value="10">10th <option value="11">11th <option value="12">12th <option value="13">13th <option value="14">14th <option value="15">15th <option value="16">16th <option value="17">17th <option value="18">18th <option value="19">19th <option value="20">20th <option value="21">21st <option value="22">22nd <option value="23">23rd <option value="24">24th <option value="25">25th <option value="26">26th <option value="27">27th <option value="28">28th <option value="29">29th <option value="30">30th <option value="31">31st </select> <select name="endmonth"> <option value="01">january <option value="02">february <option value="03">march <option value="04">april <option value="05">may <option value="06">june <option value="07">july <option value="08">august <option value="09">september <option value="10">october <option value="11">november <option value="12">december </select> <select name="endyear"> <option>1990 <option>1991 <option>1992 <option>1993 <option>1994 <option>1995 <option>1996 <option>1997 <option>1998 <option>1999 <option>2000 <option>2001 <option>2002 <option>2003 <option>2004 <option>2005 <option>2006 <option>2007 <option>2008 <option>2009 </select> <input type="hidden" name="startdate"> <input type="hidden" name="enddate"> <p> <input type="button" onClick="validate(this.form)" value="Validate"> </form>