Regex for Social Security Number — Pattern Explained with Examples
This regex validates US Social Security Numbers (SSNs) in the standard XXX-XX-XXXX format. Beyond checking the nine-digit structure with hyphens, the SSN format has specific rules about which numbers are valid: area numbers cannot be 000, 666, or 900–999, and group and serial numbers cannot be all zeros. Proper SSN validation is critical for payroll, benefits administration, and identity verification systems.
The Pattern
/^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/Pattern Breakdown
| Part | Meaning |
|---|---|
^ | Start of string |
(?!000|666|9\d{2}) | Negative lookahead: area not 000, 666, or 900–999 |
\d{3} | Exactly 3 digits (area number) |
- | Literal hyphen separator |
(?!00) | Negative lookahead: group not 00 |
\d{2} | Exactly 2 digits (group number) |
- | Literal hyphen separator |
(?!0000) | Negative lookahead: serial not 0000 |
\d{4} | Exactly 4 digits (serial number) |
$ | End of string |
Matches
123-45-6789987-65-4321001-01-0001999-99-9999(hypothetical — real 999 area is not issued)555-55-5555
Does NOT Match
123-45-678— only 8 digits123456789— no hyphens123-45-67890— extra digit123 45 6789— spaces instead of hyphensABC-DE-FGHI— non-numeric000-00-0000— area 000, group 00, serial 0000 all invalid666-12-3456— area 666 is invalid987-00-1234— group 00 is invalid
Language Examples
JavaScript
const ssnRegex = /^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
console.log(ssnRegex.test('123-45-6789')); // true
console.log(ssnRegex.test('000-00-0000')); // false
console.log(ssnRegex.test('666-12-3456')); // false
// Strip hyphens for storage
const raw = '123-45-6789'.replace(/-/g, '');
console.log(raw); // 123456789
Python
import re
ssn_regex = r'^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$'
print(bool(re.match(ssn_regex, '123-45-6789'))) # True
print(bool(re.match(ssn_regex, '000-00-0000'))) # False
print(bool(re.match(ssn_regex, '666-12-3456'))) # False
# Strip hyphens
raw = re.sub(r'-', '', '123-45-6789')
print(raw) # 123456789Common Pitfalls
000, 666, 900–999 area numbers are invalid — The SSA never issues area numbers in these ranges. The negative lookahead
(?!000|666|9\d{2})blocks them.00 group number and 0000 serial are invalid — These are never assigned. The lookaheads
(?!00)and(?!0000)handle these cases.SSN randomization since 2011 — In 2011, the SSA introduced randomized SSN assignment, eliminating the geographic significance of the area number. Pre-2011 area numbers corresponded to the applicant’s state of application; post-2011 they are random.
Dashes or no dashes — This pattern requires dashes. If you need to accept both, strip dashes first or use
/^\d{9}$/for raw format. Never display or store SSNs without encryption.
Real-World Use Cases
- Payroll processing — Validating employee SSNs for tax reporting (W-2 forms)
- Benefits enrollment — Verifying identity in healthcare or government benefits systems
- Background checks — Screening applicant SSNs against credit bureaus and public records
FAQ
Related Patterns
- Regex for ZIP/Postal Code
- Regex for Password Strength
- Regex for Username/Slug
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro