Reply
Date validation
Old 02-22-2007, 02:22 PM Date validation
The_Anomaly's Avatar
Extreme Talker

Posts: 201
Location: Boston, Ma
I was wonder how I would go about making sure something within a text box is a valid date. I know that usually I would use JavaScript but I don’t know if that’s possible to use within an asp page. I would also imagine that using asp there would be another way of validating a date anyway without have to introduce another language.

From various Google searches I have found a few things that didn’t work. Mainly people are saying use the IsDate function, but that won’t tell you if it’s a VALID date but only if it IS a date.

I’m also looking to see if the dates not in the past, I don’t think this will be hard after I find a way to validate it though.

Other information: The format will be mm/dd/yyyy

__________________
Forum Admin (taking no credit for design): http://rsfreedom.com/
The_Anomaly is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 02-22-2007, 02:53 PM Re: Date validation
Learning Newbie's Avatar
Moderator

Posts: 5,199
Name: John Alexander
First, IsDate will tell you whether something is a valid date or not, it's just you have your own definition of valid. But try passing January 41, 2104 and it'll say no way Jose. Also, JavaScript is indeed the way to go here, because it can be done on the client end without having to make a round trip to your web server. And absolutely you can mix JS and ASP. But you seem uncomfortable with the idea, so I'll show you how to do it in ASP using VBScript, and then you can move on when you're ready.

Public Function IsOkayDate(someValue As String) As Boolean
If IsDate(someValue) Then If CDate(someValue) > Now() Then If Format(CDate(someValue), "mm/dd/yyy") = someValue Then Return True
Return False
End Function

Okay, what do I win?
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 02-22-2007, 02:56 PM Re: Date validation
chrishirst's Avatar
Super Moderator

Posts: 13,576
Location: Blackpool. UK
Use a regular expression
The pattern for mm/dd/yyyy is
Code:
"(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d"

and a function from my code library is;
Code:
function CheckDate(strIn)
' 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(strIn)
	set objRE = nothing
end function
It will return true is the date is valid and false is not
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-22-2007, 04:00 PM Re: Date validation
The_Anomaly's Avatar
Extreme Talker

Posts: 201
Location: Boston, Ma
It seems my problem now is how do i call this. I was thinking, on the submit i would move to an invisible page that would do all of the validation then if its valid redirect to the next page, if it wasnt go back and show where the errors were. cleint side/server side isnt a big deal since this will be for an in-house help desk site for a company of ~60
__________________
Forum Admin (taking no credit for design): http://rsfreedom.com/
The_Anomaly is offline
Reply With Quote
View Public Profile
 
Old 02-23-2007, 12:07 AM Re: Date validation
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
http://truegeeks.com/asp/aspexamples/gendate/edate1.asp <--- I don't normally recommend scripts, but this one isn't bad as long as you tweak it a bit.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Reply     « Reply to Date validation
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13512 seconds with 12 queries