Identity
India Aadhaar Number Shape Regex
An Aadhaar number is a 12-digit identifier that is usually printed in three groups of four digits. Aadhaar numbers do not start with 0 or 1, so this pattern requires the first digit to be between 2 and 9 and allows an optional single space between groups. It is handy for KYC forms, fintech onboarding and masking routines that need to spot Aadhaar-like values. The important limit is the checksum: real Aadhaar numbers end with a Verhoeff check digit, which a regular expression cannot compute, so run that check separately in code.
Pattern (JavaScript)
/^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$/Starts at the start of the line/string, then one of the characters 2-9, then a digit, exactly 3 times, then a space, optionally (zero or one time), then a digit, exactly 4 times, then a space, optionally (zero or one time), then a digit, exactly 4 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid, and it does not compute the Verhoeff check digit.
Examples
Matches
- ✓ 2345 6789 0123
- ✓ 987654321012
- ✓ 4123 4567 8901
Does not match
- ✗ 1234 5678 9012
- ✗ 0123 4567 8901
- ✗ 2345 6789 012
Try it
0 matches (JavaScript engine)
2345 6789 0123
987654321012
4123 4567 8901
1234 5678 9012
0123 4567 8901
2345 6789 012Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$/g;
const text = "2345 6789 0123";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$')
text = '2345 6789 0123'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$/';
$text = '2345 6789 0123';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$");
String text = "2345 6789 0123";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$`)
text := "2345 6789 0123"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$");
var text = "2345 6789 0123";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$").unwrap();
let text = "2345 6789 0123";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[2-9][0-9]{3} ?[0-9]{4} ?[0-9]{4}$/
text = '2345 6789 0123'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why can the first digit not be 0 or 1?
- Aadhaar numbers are generated so that they never begin with 0 or 1. Rejecting those leading digits filters out many mistyped or padded values before any further checking.
- How do I verify the Verhoeff check digit?
- The Verhoeff algorithm uses multiplication and permutation tables that regex cannot express. After the pattern matches, remove the spaces and run a short Verhoeff function in your application code.
- Should I store Aadhaar numbers after matching them?
- Store them only when you have a legal basis, and prefer masked forms such as showing only the last four digits. The regex is equally useful for finding and masking numbers in logs or documents.