Date Validation

[Total: 0    Average: 0/5]

This script will check 3 dropdown boxes dynamically created in ASP.

Head Code:

<SCRIPT LANGUAGE=javascript> 
<!-- 
function dateCheck(m, d, y) 
{ 
// If nothing is selected the database will enter a Null value 
if(m.value != ''){ 
//Check for months with only 30 days 
if(m.value == "4" || m.value == "6" || m.value == "9" || m.value == "11"){ 
if(d.value == "31"){ 
alert("There are only 30 daysnin this month") 
d.focus(); 
return false; 
} 
} 
if(m.value == "2"){ //Month of February 
if(parseInt(y.value)%4 != 0 && d.value == "29"){ // If the year is divisible by 4 then it is a leap year 
alert("There are only 28 days in the month nof February unless it is a leap year.") 
d.focus(); 
return false; 
} 
else if(d.value == 30 || d.value == 31){ // There are never more than 29 days in February 
alert("There are never more than 29 daysnin the month of February") 
d.focus(); 
return false; 
} 
} 
return true; 
} 
} 

function checkAllDates() 
{ 
// Do multiple checks if there is more than one date to evaluate 
if(!dateCheck(document.frmView.cmbDCM, document.frmView.cmbDCD, document.frmView.cmbDCY)){ 
} 
else if(!dateCheck(document.frmView.cmbTSM, document.frmView.cmbTSD, document.frmView.cmbTSY)){ 
} 
else 
{alert("All dates are valid!")} 
} 


//--> 

</SCRIPT> 

Body Code:

<FORM name=frmView id=frmView> 
<CENTER><H2>Date Validation</H2> 
<TABLE> 
<TR> 
<TD>Dev. Complete Date:</TD> 
<TD> 
<SELECT id=cmbDCM name=cmbDCM> 
<OPTION value="" selected>-- MONTH --</OPTION> 
<%For i = 1 To 12%> 
<OPTION value="<%=i%>"><%=i%></OPTION> 
<%Next%> 
</SELECT> 
<SELECT id=cmbDCD name=cmbDCD> 
<OPTION value="" selected>-- DAY --</OPTION> 
<%For j = 1 To 31%> 
<OPTION value="<%=j%>"><%=j%></OPTION> 
<%Next%> 
</SELECT> 
<SELECT id=cmbDCY name=cmbDCY> 
<OPTION value="" selected>-- YEAR --</OPTION> 
<%For y = CDbl(Datepart("yyyy", Date())) To CDbl(Datepart("yyyy", Date()) + 5)%> 
<OPTION value="<%=y%>"><%=y%></OPTION> 
<%Next%> 
</SELECT> 
</TD> 
</TR> 
<TR> 
<TD>Test Start Date:</TD> 
<TD> 
<SELECT id=cmbTSM name=cmbTSM> 
<OPTION value="" selected>-- MONTH --</OPTION> 
<%For i = 1 To 12%> 
<OPTION value="<%=i%>"><%=i%></OPTION> 
<%Next%> 
</SELECT> 
<SELECT id=cmbTSD name=cmbTSD> 
<OPTION value="" selected>-- DAY --</OPTION> 
<%For j = 1 To 31%> 
<OPTION value="<%=j%>"><%=j%></OPTION> 
<%Next%> 
</SELECT> 
<SELECT id=cmbTSY name=cmbTSY> 
<OPTION value="" selected>-- YEAR --</OPTION> 
<%For y = CDbl(Datepart("yyyy", Date())) To CDbl(Datepart("yyyy", Date()) + 5)%> 
<OPTION value="<%=y%>"><%=y%></OPTION> 
<%Next%> 
</SELECT> 
</TD> 
</TR> 
</TABLE><BR> 
<INPUT type="button" value="Check Dates" id=button1 name=button1 onClick="return checkAllDates()"> 
</FORM> 

Times Viewed: 8

Leave a Reply

Your email address will not be published. Required fields are marked *