Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
A textbox in an HTML Form has 01/04/2001 (in DD-MM-YYYY) format as its default value which can be changed by the user. In order to ensure that users enter a valid date (for eg. 41/04/2001 would be invalid!!), how do I validate the date using ASP? What I tried is this:

<%
Dim dtFPeriod
dtFPeriod=Request.Form(&quot;fperiod&quot;)

If(Month(dtFPeriod)=1 OR Month(dtFPeriod)=3 OR Month(dtFPeriod)=5 OR Month(dtFPeriod)=7 OR Month(dtFPeriod)=8 OR Month(dtFPeriod)=10 OR Month(dtFPeriod)=12) Then
If(Day(dtFPeriod)>31 OR Day(dtFPeriod)<=0) Then
Response.Write(&quot;Invalid Date&quot;)
End If
ElseIf(Month(dtFPeriod)=4 OR Month(dtFPeriod)=6 OR Month(dtFPeriod)=9 OR Month(dtFPeriod)=11) Then
If(Day(dtFperiod)>30 OR Day(dtFPeriod)<=0) Then
Response.Write(&quot;Invalid Date&quot;)
End If
End If

If Not(Year(dtFPeriod) Mod 4=0) Then
If(Month(dtFPeriod)=2) Then
If(Day(dtFPeriod)>28 OR Day(dtFPeriod)<=0) Then
Response.Write(&quot;Invalid Date&quot;)
End If
End If
Else
If(Month(dtFPeriod)=2) Then
If(Day(dtFPeriod)>29 OR Day(dtFPeriod)<=0) Then
Response.Write(&quot;Invalid Date&quot;)
End If
End If
End If
%>

Now if a valid date is entered by the user, then there is no problem but if, say, a user enters the date as 41/04/2001 then ASP throws the following error:

Type Mismatch: '[string: &quot;41/04/2001&quot;]'

which points to the very first If condition. So how do I validate the date users input using ASP?

Thanks,

Arpan
 
How about using this code to ensure the date is always valid, leaving no room for error even where theres only 30 days in a month etc

<form name=&quot;dateForm&quot; method=&quot;&quot; action=&quot;&quot;>
<body onload=&quot;setCurrentDate();&quot;>
<SCRIPT LANGUAGE='JavaScript'>
<!--
function setCurrentDate() {
var currentDate = new Date();
document.dateForm.inyear.selectedIndex = 0;
document.dateForm.inmonth.selectedIndex = currentDate.getMonth();
setDays();
document.dateForm.inday.selectedIndex = currentDate.getDate() - 1;}
function setDays() {
var y = document.dateForm.inyear.options[document.dateForm.inyear.selectedIndex].value;
var m = document.dateForm.inmonth.selectedIndex;
var d;
if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) {
days = 30;}
else if (m == 1) {
if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
days = 29
else
days = 28}
else {
days = 31;}
if (days > document.dateForm.inday.length) {
for (i = document.dateForm.inday.length; i < days; i++) {
document.dateForm.inday.length = days;
document.dateForm.inday.options.text = i + 1;
document.dateForm.inday.options.value = i + 1;}}
if (days < document.dateForm.inday.length) {
document.dateForm.inday.length = days;
if (document.dateForm.inday.selectedIndex == -1)
document.dateForm.inday.selectedIndex = days - 1;}}
//-->
</SCRIPT>
Check-in DateMonth
<SELECT NAME=&quot;inmonth&quot; onChange=&quot;setDays();&quot;>
<OPTION VALUE=&quot;January&quot;>January<OPTION VALUE=&quot;February&quot;>February<OPTION VALUE=&quot;March&quot;>March<OPTION VALUE=&quot;April&quot;>April
<OPTION VALUE=&quot;May&quot;>May<OPTION VALUE=&quot;June&quot;>June<OPTION VALUE=&quot;July&quot;>July<OPTION VALUE=&quot;August&quot;>August
<OPTION VALUE=&quot;September&quot;>September<OPTION VALUE=&quot;October&quot;>October<OPTION VALUE=&quot;November&quot;>November
<OPTION VALUE=&quot;December&quot;>December</SELECT>
Day
<SELECT NAME=&quot;inday&quot;>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
for (i = 1; i < 10; i++) {
document.writeln('<OPTION VALUE=&quot;' + i + '&quot;>' + i);}
</SCRIPT></SELECT>
Year
<SELECT NAME=&quot;inyear&quot; onChange=&quot;setDays();&quot;>
<SCRIPT LANGUAGE='JavaScript'>
<!--
var currentDate = new Date();
var currentYear = currentDate.getYear();
if (currentYear < 2000) {
currentYear += 1900;}
for (i = 0; i < 2; i++) {
document.writeln('<OPTION VALUE=&quot;' + currentYear + '&quot;>' + currentYear);
currentYear++;}
// -->
</SCRIPT>
</SELECT></body></form> Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
I think you can replace all your code by :
Code:
if not isDate(dtFPeriod) then
  Response.Write(&quot;Invalid Date&quot;)
End If
Water is not bad as long as it stays out human body ;-)
 
Hi Targol,

How stupid of me??? Immediately after posting my question, it suddenly struck be that there's something called the IsDate() function in ASP......readymade!!!!

Anyway, thanks for your response.

Hey Gary, thanks for your response as well!!!

Regards,

Arpan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top