South African ID Number Regex for Java
/^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching south african id 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
// South African ID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
import java.util.regex.Pattern;
public class SouthAfricanIdNumberValidator {
private static final Pattern PATTERN =
Pattern.compile("^(\\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])(\\d{4})([01])(\\d)(\\d)$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("8001015009087")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
8001015009087 | 080101500908 |
9202204720082 | 80010150090870 |
7601100800086 | 800101500908A |
| — | 800001500908 |
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
The ID number encodes race historically (a remnant of apartheid-era classification) — the digit is still present but is now always 8 for new IDs. Never use this digit for any purpose. Always validate the Luhn checksum.
Technical Notes
Structure: YYMMDD (DOB) + SSSS (gender sequence: 0000-4999=female, 5000-9999=male) + C (citizenship: 0=SA citizen, 1=permanent resident) + A (race, now always 8) + checksum (Luhn). Checksum must be Luhn-validated.
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