Skip to content
Regex for Social Security Number — Pattern Explained with Examples

Regex for Social Security Number — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 3 min read

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

PartMeaning
^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-6789
  • 987-65-4321
  • 001-01-0001
  • 999-99-9999 (hypothetical — real 999 area is not issued)
  • 555-55-5555

Does NOT Match

  • 123-45-678 — only 8 digits
  • 123456789 — no hyphens
  • 123-45-67890 — extra digit
  • 123 45 6789 — spaces instead of hyphens
  • ABC-DE-FGHI — non-numeric
  • 000-00-0000 — area 000, group 00, serial 0000 all invalid
  • 666-12-3456 — area 666 is invalid
  • 987-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)  # 123456789

Common Pitfalls

  1. 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.

  2. 00 group number and 0000 serial are invalid — These are never assigned. The lookaheads (?!00) and (?!0000) handle these cases.

  3. 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.

  4. 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

No. It validates the format and known invalid number ranges, but a number that passes the regex may never have been issued by the SSA. Use the Social Security Administration’s verification service (or a third-party API) to confirm actual issuance.
Strip hyphens and store as a 9-character numeric string in an encrypted column (e.g., AES-256). Never log, display in full, or transmit SSNs over unencrypted channels. Display only the last four digits when necessary.

Related Patterns

Previous Regex for ZIP/Postal Code — Pattern Explained with Examples Next Regex for Hex Color Codes — Pattern Explained with Examples

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro

Home Browse Regex Pattern Library