Regex for Age Range (0 to 100)

Users are often required to enter their ages into online forms. The validity of this input can usually be checked by comparing the numeric value of the text entered. But regular expressions is another alternative for this.

A regular expression that validates an age in a specific age range must check that only digits are entered. It must be able to match single digits (1 to 9), double digits (10 to 99) and certain triple digits (depending on the maximum allowable age).

An expression that will match ages in the range 0 to 100 is:

/^100|[1-9]?\d$/Edit with Regexity

Let’s break down how this works:

Age Range 0 to 100

We need to split our regex into two parts, one of which will match single digits and double digits, and another which will match triple digits. We will start with the triple digits.

In this case, the only triple-digit number that we will allow is 100Edit with Regexity, since that is the maximum age in our range:

/100/

The regular expression above will match exactly 100Edit with Regexity.

Next, we add an expression that will match the double digits, consisting of two digit characters \dEdit with Regexity which will each match any digit from 0 to 9:

/100|\d\d/Edit with Regexity

The double-digit expression is separated from the triple-digit expression using the OR symbol |Edit with Regexity or pipe character, to indicate that we will allow matching either one or the other.

Note that the two digit characters \d\dEdit with Regexity will also match single digits with a leading zero, such as 01Edit with Regexity or 08Edit with Regexity. In some instances, you might want to allow this, as many programming languages will automatically drop the leading zero when converting the string to an integer. However, if we want to match two-digit numbers with no leading zeros, we can change the first digit symbol to a range from 1 to 9 in square brackets:

/100|[1-9]\d/Edit with Regexity

The square brackets indicate that we will accept any digit from 1 to 9.

Next, we need to deal with the single-digit numbers. One way to do this is to add another digit character \dEdit with Regexity separated by an OR character |Edit with Regexity:

/100|[1-9]\d|\d/Edit with Regexity

However, a more compact way of doing this is to simply make the first character in the two-digit expression optional. We can do this by placing a zero-or-one quantifier (written as ?Edit with Regexity) behind the square brackets:

/100|[1-9]?\d/Edit with Regexity

Note that the expression above will also match 0Edit with Regexity. If you want to enforce an age greater or equal to 1, you will need to revert to the previous expression and specify the one-digit match as a range from 1 to 9:

/100|[1-9]\d|[1-9]/Edit with Regexity

In this expression, we want to allow ages from 0 upwards, so we will revert back to our previous expression:

/100|[1-9]?\d/Edit with Regexity

Finally, to ensure we match only the age and nothing before or after it, we can add a start-of-string ^Edit with Regexity and end-of-string character $Edit with Regexity to the front and back of the expression, respectively:

/^100|[1-9]?\d$/Edit with Regexity

Age Range 0 to 99

If we want to match only ages from 0 to 99, we can use the same approach as above, and simply remove the 3-digit 100Edit with Regexity from our expression.

/^[1-9]?\d$/Edit with Regexity

If you don’t want to accept 0Edit with Regexity as an input, you can add a range from 0 to 9 behind this expression, separated by an OR symbol |Edit with Regexity:

/^[1-9]?\d|[1-9]$/Edit with Regexity

Age Range 0 to 200

To accept ages in the range from 0 to 200, we will need to adjust the regular expression above to also match all three-digit numbers starting with a 1, and also the number 200Edit with Regexity.

We will start with the number 200Edit with Regexity by itself:

/200/Edit with Regexity

Next, we can use the two-digit expression we created above and simply add an optional 1Edit with Regexity in front of it. We do this by inserting a 1Edit with Regexity followed by a zero-or-one quantifier (written as ?Edit with Regexity) before the two-digit expression:

/200|1?[1-9]?\d/Edit with Regexity

Note that we once again separate the 200Edit with Regexity from the rest using an OR symbol |Edit with Regexity to show that we will accept one or the other.

However, note that the expression above will not accept three-digit numbers of which the middle character is a 0Edit with Regexity (such as 101Edit with Regexity or 105Edit with Regexity). To fix this, we must change the second character to a digit character \dEdit with Regexity which will match all digits from 0 to 9 (keeping the zero-or-one quantifier in place):

 /200|1?\d?\d/Edit with Regexity

As explained in the first section, note that the expression above will now also match single-digits with leading zeros as well as two consecutive zeros 00Edit with Regexity.

To prevent any numbers with leading zeros to be accepted, we need to separate the three-digit expressions completely from the two-digit expressions:

/200|1\d\d|[1-9]?\d/Edit with Regexity

This expression is divided into three parts separated by the OR character |Edit with Regexity. The first part accepts only the number 200Edit with Regexity. The second part accepts all numbers from 100Edit with Regexity to 199Edit with Regexity by specifying a leading digit 1Edit with Regexity followed by two digit characters \d\dEdit with Regexity which match all digits from 0 to 9. The final part accepts all two-digit and one-digit numbers from 0 – 99 by specifying a leading digit 1Edit with Regexity to 9Edit with Regexity which is optional, followed by a digit character \dEdit with Regexity that accepts all digits from 0 to 9.

As always, to ensure we match only the age and no character before or after it, we can add a start-of-string ^Edit with Regexity and end-of-string character $Edit with Regexity to the front and back of the expression, respectively:

/^200|1\d\d|[1-9]?\d$/Edit with Regexity

Which Flags to Use

Since we are matching only digits and no alphabetic characters, the case insensitive flag iEdit with Regexity will make no difference and can therefore be left out.

If you want to match all instances of the digits between 0 and 100 in a piece of text, instead of validating a single input, you can do so by specifying the global flag gEdit with Regexity and remove the start-of-string and end-of-string characters:

/100|[1-9]?\d/gEdit with Regexity
Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment