简体   繁体   中英

How to validate 12-Hour Time with regex (regular Expression)

I want to validate a 12-Hours time format like (01:05 PM) in PHP, Javascript and HTML.

I tried this:

((([1-9])|(1[0-2])):([0-5])(0|5)\s(A|P)M)

But I don't know why it doesn't work.

Try ,

^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$ 

You may test here

try this :

(((0[1-9])|(1[0-2])):([0-5])([0-9])\\s(A|P)M)

test regex here

try this for case sensitive AM|PM|am|pm : `

(((0[1-9])|(1[0-2])):([0-5])(0|5)\\s(A|P|a|p)(M|m))

`

这将适用于 pm/am 和 PM/AM 并带有或不带有 0(零)前缀

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM