Indian Aadhaar Number Regex for Java
/^[2-9][0-9]{3}[\s-]?[0-9]{4}[\s-]?[0-9]{4}$/What this pattern does
This page provides a well-structured, multi-part regular expression for matching indian aadhaar number, ported and verified for Java. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Indian Aadhaar Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
import java.util.regex.Pattern;
public class IndianAadhaarNumberValidator {
private static final Pattern PATTERN =
Pattern.compile("^[2-9][0-9]{3}[\\s-]?[0-9]{4}[\\s-]?[0-9]{4}$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("234567890123")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
234567890123 | 034567890123 |
2345 6789 0123 | 134567890123 |
2345-6789-0123 | 23456789012 |
9999 9999 9999 | 2345678901234 |
| — | ABCD 5678 9012 |
When to use this pattern
This pattern is drawn from the Identity & PII > National Identity Numbers category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Aadhaar numbers must not be publicly displayed or shared without consent under the Aadhaar Act. UIDAI provides a Masked Aadhaar format (XXXX XXXX 1234) for semi-public use. Virtual IDs (VID) are the preferred sharing mechanism.
Technical Notes
Aadhaar UIDs are issued by UIDAI to Indian residents. First digit cannot be 0 or 1 (a design choice to avoid confusion). The 12-digit number uses a Verhoeff checksum algorithm. Display format is typically XXXX XXXX XXXX.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern