An Aadhaar number is a 12-digit code assigned to each individual in India. How do we write a regular expression for this code?

A regular expression for Aadhaar numbers should match a 12-digit number of which the first digit can be 0 or 1, and the rest can be any random digit. It should match versions that separate each group of 4 digits with a space or dash as well as others that use no separator.

`/^\d{3}[\s-]?\d{4}[\s-]?\d{4}\$/Edit with Regexity`

Let’s investigate how this code is put together:

## General Format of an Aadhaar Number

An Aadhaar number is generally formatted in the following way:

• Starts with the character 0 or 1 to indicate the version of the number (this might change in the future if new versions are released)
• Followed by 11 random digits
• The entire string of 12 digits can be separated by a space or a dash or can be written without a separator.

The last digit is a checksum digit, which is used to check the validity of the previous 11 digits using the Verhoeff algorithm.

## Regular Expression for Aadhaar Number

The first digit of the Aadhaar number can be either a 0 or a 1, depending on the version being used (keep in mind that future versions may start with a different number). Thus, we specify a 0 and 1 and square brackets:

`//Edit with Regexity`

The square brackets indicate that any value inside the square brackets is acceptable.

The first digit is then followed by 11 other digits, all of which can be any digit from 0 to 9. We could specify this using the digit symbol (\d), followed directly by an {11} in curly brackets to indicate that we want to match exactly 11 digits:

`/\d{11}/Edit with Regexity`

However, this will only match a 12-digit Aadhaar number when all the numbers are written together (for example 029483957293). We want to split these up into groups of 4 so we can specify a delimiter later on:

`/\d{3}\d{4}\d{4}/Edit with Regexity`

Now we have three groups of 4 digits (of which the first one is made up of either 0 or 1 followed by 3 more digits).

As mentioned, the groups of 4 digits can be separated by a dash (-) or a space (indicated in regex by \s). We can specify these characters in square brackets between the 4 digit groups.

`/\d{3}[\s-]\d{4}[\s-]\d{4}/Edit with Regexity`

However, we need to make these dashes and spaces optional. We can do this by placing a zero-or-more quantifier (?) behind both sets of square brackets.

`/\d{3}[\s-]?\d{4}[\s-]?\d{4}/Edit with Regexity`

And finally, to ensure that we match only the Aadhaar number, and nothing in front or behind it, we must add the front-of-string (^) and end-of-string (\$) characters to the beginning and end of the expression, respectively.

`/^\d{3}[\s-]?\d{4}[\s-]?\d{4}\$/Edit with Regexity`

And now it’s complete. This expression can now match the following Aadhaar numbers:

• 039428397825
• 0394 2839 7825
• 0394-2839-7825

## Which Flags to Use

Since Aadhaar numbers are entirely numeric (with optional spaces or dashes), there is no need to use the case insensitive flag (i) for this regular expression.

The expression given above will validate a given input as an Aadhaar number. However, if you’d like to change the expression to search for all Aadhaar numbers in a body of text, you should include the global flag (g) at the end, to ensure that the expression matches not only the first occurrence. In this case, you should also remove the start-of-string and end-of-string characters.

`/\d{3}[\s-]?\d{4}[\s-]?\d{4}/gEdit with Regexity`

## Further Considerations

Regular expressions can validate the format of an Aadhaar number but are not powerful enough to ensure its validity. This can be done by evaluating the checksum digit (last digit) using the Verhoeff algorithm to check the integrity of the entire number. This is beyond the scope of this article but can be done using your favorite programming language such as Javascript or Python.

## Sources

The regular expressions on this page were adapted from solutions posted on Stack Overflow by Rajkumar Somasundaram on this question and by aaron_world_traveler on this question. Additional articles here, here, and here were also helpful.

The following links proved valuable in understanding the general format of an Aadhaar number: ##### Benjamin

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

### 1 thought on “Regex for Aadhaar numbers”

1. There is an error. Aadhar numbers CANNOT begin with a zero or 1, so that makes this regex invalid!