Tad
Tad

Reputation: 129

ExtJS 4 textfield regex condition

I can't solve simple regex pattern in ExtJS 4 texfield.

I have to enter hour and minute in format "HHMM" and I use such a regex:

/[012][0-9][0-5][05]$/

And it is working almost good> Almost, because it's possible to enter 2900. I can't set in this regex that, when the first digit is "2" the second have to be from 0 to 4 not from 0 to 9.

Be so kind as to prompt me in this case.

Upvotes: 1

Views: 362

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You may use

^(?:[01][0-9]|2[0-3])[0-5][0-9]$

See regex demo

The regex matches:

  • ^ - start of string
  • (?:[01][0-9]|2[0-3]) - two alternatives:
    • [01][0-9] - 1 or 0 followed with any one digit
    • | - or
    • 2[0-3] - 2 followed with 0, 1, 2 or 3
  • [0-5][0-9] - 0 to 5 digit followed with any 1 digit
  • $ - end of string

To include 2400 as a valid match, use

^(?:(?:[01][0-9]|2[0-3])[0-5][0-9]|2400)$

See another demo

Upvotes: 1

Related Questions