0

Validate the following time format: "HH:MM AM/PM" .
Using the following regular expression:
([0-1][1-2]):([0-5][0-9]) ?([AP][M])
It works for most of the part but for "001:00 AM" it returns true as well. Any help appreciated.

Raj
  • 991
  • 2
  • 12
  • 27
  • Thanks for replying, I am using JS, reg.test("10:00 AM") returns false. – Raj May 07 '20 at 11:37
  • Use `^(1[0-2]|0?[1-9]):([0-5][0-9]) ?([AP][M])$`, your pattern is off a bit. – Wiktor Stribiżew May 07 '20 at 11:41
  • Hi, This validates true for reg.test("1:00 AM"), but "01:00 AM" should pass ie hour and minute should be two digit. Can I reopen this?@WiktorStribiżew – Raj May 07 '20 at 11:46
  • Why reopen? It has been answered. If you need a two-digit hour only, remove `?` after `0`: `^(1[0-2]|0[1-9]):([0-5][0-9]) ?([AP][M])$`, see the top links now. – Wiktor Stribiżew May 07 '20 at 11:51

0 Answers0