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

Using Date.parse to validate time entries

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I am trying to validate a text field that must contain a valid time. I thought I could do it easily using Date.parse(). According to the documentation if just a time string is passed to the function it will return a number (milliseconds since the Epoch). I then use 'isNaN' which should be false if an invalid time string was passed (a non number was returned by Date.parse()). So, either it does not work the way I interpreted the documentation or there is a syntax problem with the string. What I am passing to test with is "08:30". Can someone help me on this one? TIA.
 
IIRC, it'll expect a date and time string. Try passing something like 01/01/1997 08:30 to see what happens.
 

If you just want to validate times in the format "xx:xx", I think it might be easier to split on the colon, and detect for the presence of a number in the resulting two array elements, and that they are within the bounds you require (0-23, 1-12, 0-59, etc).

Hope this helps,
Dan



The answers you get are only as good as the information you give!

 
theboyhope,

I thought of that myself but when I tried it, it didn't work. However, based on your suggestion, I tried it again. This time is worked so obviously I did something wrong the first time. Thanks for making me go back.
 
You could also use a regular expression to validate the time. That's part of what they're for.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top