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!

Using Javascript to Validate Format of Text Box

Status
Not open for further replies.

ejrhodes

MIS
Jul 9, 2001
20
US
I have a text box on my form that needs to be in the following format (the leading 0 is not required for mm or dd)

mm/dd/yyyy hh:mm

The hh:mm is optional, but if they put a time value in, it should be in that format.

Can someone provide me with a function I can call onblur of the textbox that will provide this validation?

Thanks
 
Assuming hh:mm is 24-hour time (no AM/PM):
Code:
function chkDate(pDate) {
myRe = new RegExp("^(0?[1-9])|(1[012])/(0?[1-9])|([12][0-9])|(3[01])/[0-9]{4}( ([01]?[0-9])|(2?[0-3]):[0-6][0-9])?$");
if ( myRe.test(pDate) )
   // valid date/time
else
   // invalid date/time
}
That regex could be a little more complete. For instance, it will allow up to 31 days in ANY month, and the year can be ANY 4 digits. But it will catch things like 0/0/1234 or 34:99 for time.


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