Regex. Match any number from 1 to 365
We are using RegEx Validator to validate the input from the text field.
The current RegEx expression checks if the current number is in the range from 1 to 999.
We just inherited this code and we need to change the range from 999 to 365. In short, we need to write a regex to check if the input is between 1 - 365.
Of course, we can just use RangeValidator or a custom validator. But we don’t want to do that because we don’t want to introduce "significant" changes.
Thanks!
EDIT:
We also need to catch the following patterns: 001 for 1019 for 19
0
a source to share
4 answers
Using regexes is certainly not the best way to test entire ranges, but here you go:
^([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$
[1-9][0-9]? matches 1 - 99
[12][0-9][0-9] matches 100 - 299
3[0-5][0-9] matches 300 - 359
36[0-5] matches 360 - 365
EDIT: with leading zeros (also matches strings like 00000000321):
^0*([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$
+12
a source to share