Time Validation | JavaScriptSource

Time Validation

Sandeep Tamhankar Sep 9, 2000
This function verifies that a string is a valid time, in the form hh:mm:ss am/pm, where seconds are optional. It accepts military time (hour between 0 and 23) as long as am/pm isn't specified. It requires am/pm when the hour is less than or equal to 12.
Time (HH:MM:SS AM/PM format)

The JavaScript Source: Forms: Time Validation

Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s). The script is yours!!!


Did you use this script?  Do you like this site?  Please link to us!

Leave a Response


I was able to crash this code by entering "asdf asdf" in the form field. I fixed this by adding a "try" statement: var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/; try { var matchArray = timeStr.match(timePat); } catch(e) { matchArray = null; }

Pappy Boyington Aug 11, 2014

Excellent post..thanks

Supreetam Apr 23, 2012