None Coder
None Coder

Reputation: 47

Validate time formatted as 1:00:00 with regex

i work with time additional for jquery validation plugin.

$.validator.addMethod("time24", function(value, element) {
    return this.optional(element) || /^([01]\d|2[0-3])(:[0-5]\d){1,2}$/.test(value);
}, "not valid time format.");

this worked Only with this time format:

00:00:00
01:00:00

But My Time format is:

0:00:00
1:00:00

Upvotes: 1

Views: 574

Answers (1)

Tilo
Tilo

Reputation: 3325

How about this regular expression:

/^([01]?\d|2[0-3])(:[0-5]\d){1,2}$/

The sub-expression [01]?\d|2[0-3] will accept 0, 1, ... 9, 10, ..., 22, 23 and 00, 01, ... is that what you want or do you not want leading zeros for the hour? In that case use:

/^(1?\d|2[0-3])(:[0-5]\d){1,2}$/

Upvotes: 3

Related Questions