I’m looking for a regular expression to match different time formats (for a time tracking web-app) in javascript. The user can insert times with one of the following formats:
h
hh:mm
hh.mm
I can easily create a regular expression to match hh:mm and hh.mm, but i can’t get the single hour format working.
This is my current regex:
([0-2][0-9])(.|:)([0-5][0-9])
Allowed character: 0-9, . and :.
If the user types any other character, the validation should fail.
Does anyone have any suggestions?
Edit
following formats should work to:
h:mm (3:30)
solution: http://regexr.com?31gc3
Not sure if
his in 24 or 12 hours format but for 24 hour this will do the job/^([2][0-3]|[01]?[0-9])([.:][0-5][0-9])?$/and for 12 hour – this/^([1][0-2]|[0]?[0-9])([.:][0-5][0-9])?$/