South African ID Number Regex for PHP
/^(\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 PHP. 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 PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// South African ID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
define('SOUTH_AFRICAN_ID_NUMBER_PATTERN', '/^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$/');
function validate_south_african_id_number(string $input): bool {
return (bool) preg_match(SOUTH_AFRICAN_ID_NUMBER_PATTERN, $input);
}
// Example
var_dump(validate_south_african_id_number("8001015009087")); // bool(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 PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. 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