0

I have referred to several SO webpages for the answer to my question, but I keep reading that regex should not be used for validating numbers which are less than or greater than a certain range. I want to ensure that a user enters numbers within the following ranges: 11--20 and 65-100. Anything less than 11 will not be allowed, anything between 21 and 64 will not be allowed and anything from 101 above will not be allowed. I realize I can write something like

                    if ($num <=10 and $num >= 21 and $num <=64 and $num >=101) {
                    $num = "";
                    $numErr = "Number must be within  specified ranges";
                    }

But what I really want is to use regex to preclude the range of numbers I do not want from being entered but I have not seen any satisfactory answers on SO. Can someone please help?

Jason12
  • 359
  • 1
  • 4
  • 9

1 Answers1

4

The regex would be less readable but like

/^(1[1-9]|20|6[5-9]|[7-9][0-9]|100)$/

Regex Demo

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52