lets say if the user choose 31 on 'day', February on 'month' and 2011 for 'year' .. it will return alert error ..
i have been trying to validate and display the error if the user choose the improper date like the example above ..
below is my script for the validation date... and it is quite easier to understand as well...
hope someone here can help me ... thank you ...
<html>
<head>
<script type="text/javascript">
<!--
function calcDate(frmDate)
{
//checks for a valid date
if (validateDate(frmDate.cboday, frmDate.cbomonth, frmDate.cboyear) == false)
{
frmDate.txtdate.value = "";
alert("Invalid Date" + "\n" + "Please Check!");
}
else
{
//if all 3 dropdowns aren't "- -" then populate txtdate textbox
if (frmDate.cboday.selectedIndex != 0 && frmDate.cbomonth.selectedIndex != 0
&& frmDate.cboyear.selectedIndex != 0)
{
frmDate.txtdate.value = frmDate.cboday.value + "/" +
frmDate.cbomonth.value + "/" + frmDate.cboyear.value;
}
else
{
frmDate.txtdate.value = "";
}
}
}
function validateDate(day, month, year)
//check correct number of day for given month/year
{
if (day.selectedIndex != 0 && month.selectedIndex != 0
&& year.selectedIndex != 0)
{
switch(month.value)
{
case "02" :
//February
if (year.value == Math.round(year.value / 4) * 4)
//leap year
{
if (day.value > 29)
{
return false;
}
}
else
{
//non-leap year
if (day.value > 28)
{
return false;
}
}
break;
case "04" :
//April
if (day.value > 30)
{
return false;
}
break;
case "06":
//June
if (day.value > 30)
{
return false;
}
break;
case "09":
//September
if (day.value > 30)
{
return false;
}
break;
case "11":
//November
if (day.value > 30)
{
return false;
}
break;
default:
//date is valid
return true;
break;
}
}
}
// -->
</SCRIPT>
</head>
<body>
<form name="frmDate">
<select name="cboDay" onchange="calcDate(frmDate);">
<option selected>- -</option>
<%
'populate day dropdown
For intCount = 1 to 31
response.write "<Option value=""" & intCount & """>" & _
intCount & "</option>"
Next
%>
</select>
<select name="cbomonth" onchange="calcDate(frmDate);">
'populate month dropdown
<option selected>- -</option>
<Option value="01">January</option>
<Option value="02">February</option>
<Option value="03">March</option>
<Option value="04">April</option>
<Option value="05">May</option>
<Option value="06">June</option>
<Option value="07">July</option>
<Option value="08">August</option>
<Option value="09">September</option>
<Option value="10">October</option>
<Option value="11">November</option>
<Option value="12">December</option>
</select>
<!--#INCLUDE FILE="ConvDate.inc"-->
<select name="cboyear" onchange="calcDate(frmDate);">
<option selected>- -</option>
<%
'populate year dropdown
intYear = ConvDate(Date, "%Y")
for intCount = (intYear - 10) to (intYear + 10)
response.write "<Option value=""" & intCount & """>" & _
intCount & "</option>"
next
%>
</select>
<br>
<input type="text" name="txtdate">
</form>
</body>
</html>
for the ConvDate.inc part
intPosItem = Instr(strFormat, "%Y")
Do While intPosItem > 0
strFormat = Left(strFormat, intPosItem-1) & _
DatePart("yyyy",strDate) & _
Right(strFormat, Len(strFormat) - (intPosItem + 1))
intPosItem = Instr(strFormat, "%Y")
Loop
thanks once again !
This post has been edited by Dormilich: 15 December 2011 - 04:12 AM
Reason for edit:: fixed code tag

New Topic/Question
Reply


MultiQuote







|