South African ID Number Regex for Go
/^(\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 Go. 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 Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// South African ID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
package validation
import "regexp"
var southAfricanIdNumberRe = regexp.MustCompile(`^(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])(\d{4})([01])(\d)(\d)$`)
func ValidateSouthAfricanIdNumber(s string) bool {
return southAfricanIdNumberRe.MatchString(s)
}
// Example
// fmt.Println(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 Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. 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