Blyde
Blyde

Reputation: 3153

Time regular expression

What is the javascript regular expression for 24H time? Such as:

18:30

I need to assign the regex string to a variable to be used over and over.

Thank you

Upvotes: 0

Views: 150

Answers (2)

sachleen
sachleen

Reputation: 31131

You can use

^([01]\d|2[0-3]):?([0-5]\d)$

It will require either a 0 or 1, followed by any digit (00 to 10 hours) or a 2 followed by 0, 1, 2, 3 (20 to 23 hours), an optional colon (take out the ? to make it required) and then a 0-5 followed by any digit (minutes).

Upvotes: 0

roim
roim

Reputation: 4930

Use

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

Upvotes: 6

Related Questions