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

Validating input date 1

Status
Not open for further replies.

biddingbong

IS-IT--Management
Sep 10, 2004
67
MU
Hello.
I've got a textbox and the user was supposed to input a date with the following format dd-mm-yy or dd/mm/yy. How can I know if the user has really put in a date and mot something like :
asdadsadfas
or
ss/f3/04?

Thanks
 
Code:
If IsDate(Request.Form("MyDate")) Then
    'It's a date
Else
    'It's not a date
End If
 
Search this forum. This topic has been addressed multiple times.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Oh, and if you trim the potential blank spaces before and after the date (that may have been entered accidentally), the application will be friendlier.
Code:
If IsDate(Trim(Request.Form("MyDate"))) Then
or better
Code:
Dim DateEntered
DateEntered = Trim(Request("MyDate"))

If IsDate(DateEntered) Then
...
 
will IsDate check dd-mm-yy or mm-dd-yy???

Known is handfull, Unknown is worldfull
 
but the problem is that the IsDate function will only check for a valid date but not the format...
Im using the dd-mm-yy(UK format) everywhere.
In my Db I have this date 23-05-81
But when displaying on ASP, I get this 5/23/1981
Both my pc and the Db are set to the uk format.
If IsDate only checks for a valid date, is there a function to convert US to UK date?
 
>>But when displaying on ASP, I get this 5/23/1981

how do u display? do u use some kind of function???

Known is handfull, Unknown is worldfull
 
It will check based on your server's settings. You might be able to change it on a per-page basis... I seem to recall some kind of tag that set's the page's country.
 
I don't use any function, it's direct

Udob = objRS!Dob

Where Udob is a variable and objRS is my recordset and Dob is a field(date/time) from the Db.
 
I've already tried session.LCID = 2057 but still the same result
 
o.k, i will give u a workaround:
Udob1 = split(objRS!Dob,"/")
Udob=udob1(1)&"/"&udob1(0)&"/"&udob1(2)


but i suggest u try to find the error (atleast i am not able to do it)...

Known is handfull, Unknown is worldfull
 
You could do it manually, creating a string, like:
Code:
UKDate = Day(MyDate) & "/" & Month(MyDate) & "/" & Year(MyDate)
 
Genimuse i have used that code myself but still am baffled as to how it does it? how does it differentiate between mm-dd-yy and dd-mm-yy.

e.x:
12/7/2004

it could be either mm-dd-yy or dd-mm-yy...

Known is handfull, Unknown is worldfull
 
It doesn't "know" anything about the date, really. All that does is put what the server thinks is the day first. It can be completely wrong. That line of code only works if you know that the date is in the American format and the server is interpreting dates in the American format. If the date is in the British format and the server is interpreting it in the American format then my line of code won't work, yours is the way to go.
 
biddingbong,

You can also do the validation on the client-side using JavaScript. This way, the user can re-type the date without submitting the form to the web server, making the validation procedure faster and frees the server from doing unnecessary processes.
 
I prefer a combination of the two, myself. Javascript on the client to validate, then check it again on the server. You never know if the user will have switched off Javascript (or is using a device that doesn't support it), so you'll still want to return an error, plus you need to protect yourself from malicious users/hackers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top