Im trying to implement a Regex for HH:MM:SS, example:
07:15:30
But I can't get it work
Right now Im using:
([01]?[0-9]|2[0-3]):[0-5][0-9]
Any clue?
^(?:([01]?\d|2[0-3]):([0-5]?\d):)?([0-5]?\d)$
Explanation:
^ # Start of string
(?: # Try to match
([01]?\d|2[0-3]): # HH:
([0-5]?\d): # MM:
)? # (entire group)
([0-5]?\d) # SS
$ # End of string
Used some expressions from the regex cheat sheet here:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
EDIT #1
^(?:([01]?\d|2[0-3]):([0-5]?\d):)?([0-5]\d)$
Removed the ?
on the SS
to make it return false if only 1 digit is entered for seconds.
EDIT #2
^([0-5]\d):([0-5]\d):([0-5]\d)$
Changed the HH
part a little so if the user types just HH
without the MM
and SS
, it returns false.
EDIT #3
Thanks to this comment for this expression to add to this list
^((?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With