user1400191
user1400191

Reputation: 169

how to solve time regexp

After reviewing Time validation question by Amra

I have created a regexp for my needs

var re = /^\s*([01]?\d|1[0-2]):?([00,30]\d)\s*$/;

my intention is to allow only the hour and half an hour interval times

ie: "12:00","01:00","12:30","01:30", etc

This regexp almost works, it returns false when "01:10" but it returns the value when i enter "01:01"...."01.09", it has to be false.

Please help me

And please describe this regexp in detail..

Upvotes: 0

Views: 103

Answers (2)

Jules
Jules

Reputation: 4339

If you want to allow for 24 hour time, you can change Amber's answer slightly to:

/^\s*([0-1]?\d|2[0-3]):?(00|30)\s*$/

Upvotes: 0

Amber
Amber

Reputation: 526573

[00,30] does not do what you think it does. [] groups in a regexp are character classes - sets of characters that can be matched, for instance [a-z] matches a single lowercase letter, not the string "a-z".

Try this instead:

var re = /^\s*(0?\d|1[0-2]):?(00|30)\s*$/;

(00|30) matches either 00 or 30 and nothing else - | is the regex "or" operator.

Upvotes: 1

Related Questions