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

check format (date) of passed string?

Status
Not open for further replies.

endoflux

Technical User
Aug 6, 2001
227
0
0
US
I want to check to ensure that a form field being posted to the database is a valid date. The isdate() function doesn't seem to work, given that the form field is passed to the page as a string...and if I attempt to convert an invalid format to a date, it will error out. Ideas?

Thanks!
 
Regular Expression function
Code:
function CheckDate(dtIn)
' function to validate date input
	dim objRE
	set objRE = New RegExp
	objRE.Pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d"
	objRE.Global = True
	CheckDate = objRE.test(dtIn)
	set objRE = nothing
end function


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
You could also trap the error to your advantage:

Code:
function CheckDate(dtIn)
  Dim foo

  On Error Resume Next
  foo = cDate(dtIn)  'intentional error if dtIn is not a date
  if err.number = 0 then
    CheckDate = True
  else
    CheckDate = False
    err.Clear
  end if
  
  On Error Goto 0
end function
 
I haven't tried the second solution yet, but I've tried date formats "01/01/2007" and "01/01/07" with the first solution, and both come back as invalid date formats...I'm unfortunately not talented enough to decrypt the meaning of the Pattern suggested:

objRE.Pattern = "(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d"

Anyone?
 
This solution DID work; I made another mistake: I was calling the input as if it were a variable, while it was a querystring; plugging the code below in fixed the whole deal:

If Not CheckDate(Request.Form("datein")) Then Response.Redirect ReDir4 End If

Thanks guys!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top