Reputation: 43
I have a text box. This text box should accept only values between 5 and 555. But I am unable to achieve it.
I have tried the below:
/^[0-9]{5, 500}$/
Kindly help me in this case.
Upvotes: 0
Views: 122
Reputation: 11042
This should work (though not recommended)
^(55[0-5]|5[0-4][0-9]|[1-4][0-9][0-9]|[1-9][0-9]|[5-9])$
Upvotes: 3
Reputation: 5596
What you need:
^([5-9]|\d\d|[1-4]\d\d|5[0-4]\d|55[0-5])$
It uses |
to have 3 different statements to deal with 1-digit, 2-digit and 3-digit numbers.
How it works:
^ # String starts with ...
(
[5-9] # 1-Digit - Any digit 5 or over (5 to 9)
| # OR
\d\d # 2-Digit - Any 2 digits (since any 2-digit number will be within your range)
| # OR
[1-4]\d\d # 3-Digit (below 500) - Any digit 1 to 5 (100 to 500), followed by any 2 digits
| # OR
5[0-4]\d # 3-Digit (above 500, below 550) - 5, followed by any digit 0 to 4 (500 to 540),
# followed by any digit
| # OR
55[0-5] # 3-Digits (550 or above) - 55, followed by any digit 0 to 5 (550 to 555)
)
$ # ... String ends with
Upvotes: 3