javascriptregexvalidationjquery-inputmask

Regular expression for matching time in military (24 hour) and civilian (23:59) format


I would like to create regex for validate times for military and civilian.

I just found this for military time.

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

I need to create pattern for start time and end time for military and civilian both. For example, I would like to match time in the following formats in textbox like.

How can validated in JavaScript?

Please help/suggest me to find the pattern

enter image description here


Solution

  • Here's one way of doing it.

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

    It's basically two of yours demanding a hyphen in between. It also makes the minutes optional (by having them in an optional non capturing group (?:...)?.

    Check it out here at regex101.