RegEx Numeric Check in Range?

Regular expressions work on characters (in this case digits), not numbers. You need to have a separate pattern for each number of digits in your pattern, and combine them with | (the OR operator) like the other answers have suggested. However, consider just checking if the text is numeric with a regular expression (like [0-9]+) and then converting to an integer and checking the integer is within range.


Try this

^(0?[1-9])|([1-4][0-9])|(50)$

The idea of this regex is to break the problem down into cases

  • 0?[1-9] takes care of the single digit case allowing for an optional preceeding 0
  • [1-4][0-9] takes care of all numbers from 10 to 49. This also allwows for a preceeding 0 on a single digit
  • 50 takes care of 50

Tags:

Regex