24 hour time regex for HTML 5

If you want to force to have the format HH:MM (e.g. 00:00, 23:59)

Then, you could use something like this:

/^([01]\d|20|21|22|23):[0-5]\d$/

Here is the code:

<input type="text" pattern="[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}" />

it does allow invalid hour values: 24,25,26,27,28,29, if you want to be extra correct you can do it that way:

<input type="text" pattern="([0-1]{1}[0-9]{1}|20|21|22|23):[0-5]{1}[0-9]{1}" />

I think this is a possible approach :

<input type="text" pattern="([01]?[0-9]|2[0-3]):[0-5][0-9]" id="24h"/>
<input type="text" pattern="([01]?[0-9]{1}|2[0-3]{1}):[0-5]{1}[0-9]{1}" id="24h"/>

http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/

([01]?[0-9]|2[0-3]):[0-5][0-9]

Check out this jsfiddle : example

Tags:

Html

Regex