Back to Home

Social Security Number Validation Regex (US)

CronOS Team
regexssnvalidationtutorialsocial-securityus

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

Social Security Number Validation Regex (US)

Validate US Social Security Numbers in XXX-XX-XXXX format with validation rules excluding invalid number ranges using regex pattern.

Pattern Breakdown

regex
^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
(?!000|666|9\d\d)Negative lookaheadFirst 3 digits cannot be 000, 666, or 9xx
\d{3}Area numberExactly 3 digits
-SeparatorLiteral hyphen
(?!00)Negative lookaheadGroup number cannot be 00
\d{2}Group numberExactly 2 digits
-SeparatorLiteral hyphen
(?!0000)Negative lookaheadSerial number cannot be 0000
\d{4}Serial numberExactly 4 digits
$End anchorEnsures match to string end

Detailed Breakdown

  • (?!000|666|9\d\d) - Negative lookahead for area number:
    • 000 - Invalid area number
    • 666 - Invalid area number
    • 9\d\d - Invalid area numbers 900-999
  • (?!00) - Negative lookahead: group number cannot be 00
  • (?!0000) - Negative lookahead: serial number cannot be 0000

Examples

Valid:

  • 123-45-6789
  • 001-23-4567 (area 001 is valid)
  • 555-12-3456
  • 123-01-2345 (group 01 is valid)

Invalid:

  • 000-12-3456 (area 000 is invalid)
  • 666-12-3456 (area 666 is invalid)
  • 900-12-3456 (area 900-999 are invalid)
  • 123-00-3456 (group 00 is invalid)
  • 123-45-0000 (serial 0000 is invalid)
  • 123456789 (missing hyphens)
  • 123-45-67890 (too many digits)

Implementation

JavaScript

javascript
const ssnRegex = /^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
ssnRegex.test('123-45-6789'); // true
ssnRegex.test('001-23-4567'); // true
ssnRegex.test('000-12-3456'); // false (invalid area)
ssnRegex.test('666-12-3456'); // false (invalid area)
ssnRegex.test('123-00-3456'); // false (invalid group)

Python

python
import re
ssn_regex = r'^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$'
bool(re.match(ssn_regex, '123-45-6789'))  # True
bool(re.match(ssn_regex, '001-23-4567'))  # True
bool(re.match(ssn_regex, '000-12-3456'))  # False (invalid area)
bool(re.match(ssn_regex, '666-12-3456'))  # False (invalid area)

Go

go
ssnRegex := regexp.MustCompile(`^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$`)
ssnRegex.MatchString("123-45-6789") // true
ssnRegex.MatchString("001-23-4567") // true
ssnRegex.MatchString("000-12-3456") // false (invalid area)

Limitations

  1. Format only: Validates format and invalid ranges, not actual SSN validity
  2. No database check: Doesn't verify if SSN is actually issued
  3. No historical validation: Doesn't check against historical invalid ranges
  4. Security: SSNs are sensitive PII - handle with extreme care
  5. No unformatted support: Requires hyphens (add pattern for unformatted if needed)

When to Use

  • SSN format validation in forms
  • Initial format checking
  • When you need to verify SSN structure
  • Input validation for SSN fields

For production, consider:

  • Supporting unformatted SSNs: ^(?!000|666|9\d\d)\d{3}-?(?!00)\d{2}-?(?!0000)\d{4}$
  • Masking SSNs in display (show only last 4 digits)
  • Encrypting SSNs in storage
  • Complying with privacy regulations (HIPAA, etc.)
  • Never storing full SSNs unless absolutely necessary
  • Using specialized SSN validation libraries

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern