South African ID Number Regex for JavaScript
/^(\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 JavaScript. 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 JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// South African ID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
const southAfricanIdNumberRegex = /^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$/;
function validateSouthAfricanIdNumber(input: string): boolean {
return southAfricanIdNumberRegex.test(input);
}
// Example
console.log(validateSouthAfricanIdNumber("8001015009087")); // trueTest 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 JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. 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