Regular expression for validating time in

09 Sep

( #start of group #1 1[012] # start with 10, 11, 12 | # or [1-9] # start with 1,2,...9 ) #end of group #1 : # follow by a semi colon (:) [0-5][0-9] # follw by 0..5 and 0..9, which means 00 to 59 (\s)? i) # next checking is case insensitive (am|pm) # follow by am or pm 1.

“ bm” – must end with am or pm Time12Hours is valid : am , true Time12Hours is valid : am , true Time12Hours is valid : AM , true Time12Hours is valid : pm , true Time12Hours is valid : pm , true Time12Hours is valid : PM , true Time12Hours is valid : pm , true Time12Hours is valid : am , false Time12Hours is valid : am , false Time12Hours is valid : , false Time12Hours is valid : am , false Time12Hours is valid : pm , false Time12Hours is valid : pm , false Time12Hours is valid : 0 pm , false Time12Hours is valid : am , false Time12Hours is valid : pm , false Time12Hours is valid : bm , false PASSED: Valid Time12Hours Test("am") PASSED: Valid Time12Hours Test(" am") PASSED: Valid Time12Hours Test(" AM") PASSED: Valid Time12Hours Test("pm") PASSED: Valid Time12Hours Test(" pm") PASSED: Valid Time12Hours Test(" PM") PASSED: Valid Time12Hours Test(" pm") PASSED: In Valid Time12Hours Test(" am") PASSED: In Valid Time12Hours Test(" am") PASSED: In Valid Time12Hours Test("") PASSED: In Valid Time12Hours Test(" am") PASSED: In Valid Time12Hours Test(" pm") PASSED: In Valid Time12Hours Test(" pm") PASSED: In Valid Time12Hours Test("0 pm") PASSED: In Valid Time12Hours Test(" am") PASSED: In Valid Time12Hours Test(" pm") PASSED: In Valid Time12Hours Test(" bm") =============================================== com.mkyong.regex.

regular expression for validating time in-58regular expression for validating time in-42

The following table lists the backreference constructs supported by regular expressions in . For more information, see Backreference Constructs. Back to top Alternation constructs modify a regular expression to enable either/or matching.

“1” – hour is out of range [0-23] Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , true Time24Hours is valid : , false Time24Hours is valid : , false Time24Hours is valid : 0:0 , false Time24Hours is valid : 13:1 , false Time24Hours is valid : 1 , false PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: Valid Time24Hours Test("") PASSED: In Valid Time24Hours Test("") PASSED: In Valid Time24Hours Test("") PASSED: In Valid Time24Hours Test("0:0") PASSED: In Valid Time24Hours Test("13:1") PASSED: In Valid Time24Hours Test("1") =============================================== com.mkyong.regex.

I would like a Java Script regular expression that will match time using the 24 hour clock, where the time is given with or without the colon. Hopefully didn't mess anything up along the way. This is the one I've just come up with: (^((2[0-4])|([01]? Then, parse each side and make sure that the left side is less than 24 and the right hand is less than 60.

Remove the question mark if the first 10 minutes and seconds should be written as 00 to 09.

See Recipes 2.3 and 2.12 for details on character classes and quantifiers such as the question mark.