DebugPointer
Published on

Regex for Aadhaar Number validation

Regex for Aadhaar Number validation

Aadhar number is a 12-digit unique identity number that is being provided for the citizens of India and resident foreign nationals who have spent over 182 days in the last 1 years. It can be obtained voluntarily by people. It provides several benefits to people, unifying processes across various sectors. In this article let's understand how we can create a regex for Aadhaar number and how regex can be matched for Aadhaar number.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Let's look at conditions that we have to satisfy to make sure that the Aadhaar number is valid-

Conditions to match Aadhaar Number

  • It should have 12 digits.
  • It should not start with 0 and 1.
  • It should not contain any alphabet and special characters.
  • It should have white space after every 4 digits.

Regex for checking if age is greater than 18 years

Regular Expression-

/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/gm

Test string examples for the above regex-

Input StringMatch Output
0463 2323 5754does not match
3822 4546 3674matches
34534 3233 111does not match
1244 1222 %$11does not match

Here is a detailed explanation of the above regex-

/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/gm

^ asserts position at start of the string
Match a single character present in the list below [2-9]
{1} matches the previous token exactly one time (meaningless quantifier)
2-9 matches a single character in the range between 2 (index 50) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
{3} matches the previous token exactly 3 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match aadhaar number regex pattern.