user8252938
user8252938

Reputation:

Regular expression to validate time but exclude 0:00

I'm not an expert to regular expressions, but I tried to validate the user input for a time field with it.

My current regex: /^([0-9]|0[0-9]):[0-5][0-9]$/

It should validate anything positive between 0:01 and 9:59, but it also give positive return to 0:00.

Can you please help me to modify my regular expression to exclude 0:00?

Thanks a lot!

Upvotes: 0

Views: 248

Answers (2)

bharadhwaj
bharadhwaj

Reputation: 2139

Here is the regex you are looking for.

^((?!(0:00)|(00:00)$)0?[0-9]:[0-5][0-9])$

Uses the concept of negative look ahead. Using negative lookahead, you can say match everything except 0:00 and 00:00.


Hope this helps!

Upvotes: 2

Toto
Toto

Reputation: 91488

You could use a negative lookahead:

^(?!0?0:00$)0?[0-9]:[0-5][0-9]$

Upvotes: 1

Related Questions