You are missing a set of enclosing parenthesis for the whole expression, between the start of line and end of line anchors:
^(([0-2]?[0-9]:[0-5]?[0-9])|([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]))$
Without the enclosing parenthesis, your regex was essentially saying:
- match
^([0-2]?[0-9]:[0-5]?[0-9])
- or
([0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])$
So, 00:00:99
would be valid, matching against the first part of the regex. And, so would something like: 99:00:00:00
, which would match against the second part.
That said, your regex is still going to match some unwanted patterns, such as: 29:00
An improved version would be:
^((([0-1]?[0-9])|(2[0-3]):[0-5]?[0-9])|(([0-1]?[0-9])|(2[0-3]):[0-5]?[0-9]:[0-5]?[0-9]))$