PDA

Click to See Complete Forum and Search --> : Check the date format input from an asp page


Defranko
Dec 12th, 2000, 01:42 PM
Ello,

I am writing a registration website, which takes data in and posts it to a database. Problem : if you enter an invalid date into the date of birth field it will kill the programm, is there a way to check the date which is coming into the program via a string to see if it is of a valid format (dd/mm/yy), and if so let the program deal with it.

Cheers

Dec 12th, 2000, 04:48 PM
Normally I'd use the IsDate() function to check the validity of a date (in mm/dd/yy format). I'm not sure how it'd work with your dd/mm/yy format.

One thing you could do, and this might be excessive (there is probably a better way), is to write your own date checking function. Here's an example:


<%
Function ValidDOB(dteDOB)
Dim blnReturn
Dim varDateParts

blnReturn = False

If InStr(1, dteDOB, "/") > 0 Then
varDateParts = Split(dteDOB, "/")
If Ubound(varDateParts) = 2 Then
If IsDate(varDateParts(1) & "/" & varDateParts(0) & "/" & varDateParts(2)) Then
blnReturn = True
End If
End If
End If

ValidDOB = blnReturn
End Function

If ValidDOB(Request.Form("dob")) Then
Response.Write "<P>DOB is valid!</P>" & vbcrlf
Else
Response.Write "<P>DOB is invalid!</P>" & vbcrlf
End If
%>


This routine would attempt to change a date that's formatted as dd/mm/yy into mm/dd/yy format and check it with IsDate().

If you are concerned about it being in exactly dd/mm/yy format (like 03/03/68 is valid and 3/3/68 is not) you could add some extra checks on the lengths of the parts of the varDateParts variable. Something like:


Function ValidDOB(dteDOB)
Dim blnReturn
Dim varDateParts

blnReturn = False

If InStr(1, dteDOB, "/") > 0 Then
varDateParts = Split(dteDOB, "/")
If Ubound(varDateParts) = 2 Then
If Len(varDateParts(0)) = 2 And Len(varDateParts(1)) = 2 And Len(varDateParts(2)) = 2 Then
If IsDate(varDateParts(1) & "/" & varDateParts(0) & "/" & varDateParts(2)) Then
blnReturn = True
End If
End If
End If
End If

ValidDOB = blnReturn
End Function


Like I said, there's probably a better way!

Cheers,
Paul

monte96
Dec 13th, 2000, 08:51 AM
There are a few options here, the best of which (IMHO) is to not allow the EU to enter dates in a input tag at all. Use a date picker control.

You can use the ActiveX dtpicker OR check out Dynamic Drive (http://www.dynamicdrive.com). They have a javascript calendar control that works pretty well.