RegEx - Time Validation ((h)h:mm)

后端 未结 6 1654
春和景丽
春和景丽 2021-02-13 12:46
/^\\d{1,2}[:][0-5][0-9]$/

is what I have. this limits minutes to 00-59. It does not, however, limit hours to between 0 and 12. For similarity and unifo

6条回答
  •  独厮守ぢ
    2021-02-13 13:23

    Assuming you are working in 12 hour time, 0 is not a valid hour and should also be excluded (as pointed out by Jon). Here is a basic solution:

    /^(0?[1-9]|1[012]):[0-5][0-9]$/
    

    A 24-hour time regex matcher that works similarly:

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

提交回复
热议问题