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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date validation

Status
Not open for further replies.

blueindian1

Programmer
Apr 24, 2001
150
US
hello,

anyone know the easiest way to validate that a date is in the mm/dd/yyyy format?

thanks,

rich
 
You can use IsDate(myDate) to check for a valid date, then to check for valid format, you could create a pattern using Regular Expressions. I'm not real good with those, maybe someone else can offer help there?

I do know, however, that you need at least IE5.0 installed on your server to use regular expressions.

Or you could check for valid date (shown above), then split on '/', and check that element 0 is length 1 or 2, element 1 is length 1 or 2, and element 2 is 2 or 4, depending how strict the formatting has to be. I also added checking the number of elements in the array, else you'll get an error if there aren't at least (and only) 2 '/'s in the string.

Example:

Dim arrDate
Dim sError

'set default setting
sError = False

If IsDate(myDate) = True Then
arrDate = Split(myDate, "/")
If UBound(arrDate) = 2 Then
If Len(arrDate(0)) = 2 AND Len(arrDate(1)) = 2 AND Len(arrDate(2) = 4 Then
sError = False
End If
Else
sError = True
End If
Else
sError = True
End If

If sError Then
'perform error routine for bad date
Else
'perform whatever for good date
End If



Well, that's my suggestion......
 
I missed an end paren....

If Len(arrDate(0)) = 2 AND Len(arrDate(1)) = 2 AND Len(arrDate(2)) = 4 Then
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top