Social Security Number (SSN) Validation Regular Expression

A valid U.S. Social Security Number follows the format XXX-XX-XXXX where each X is a digit. However, not all combinations are valid. The Social Security Administration has specific rules about which numbers can be assigned, making proper validation more complex than just checking the format. SSNs are a specialized type of number pattern.

Recommended Solution

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

Explanation

  • ^ - Start of the string.
  • (?!000|666|9\d{2}) - Negative lookahead to reject invalid area numbers:
    • 000 - Never assigned
    • 666 - Never assigned (historically reserved)
    • 9\\d2 - Numbers 900-999 are not assigned
  • \d{3} - Three digits for the area number (first part).
  • - - Required hyphen separator.
  • (?!00) - Negative lookahead to reject group number 00.
  • \d{2} - Two digits for the group number (middle part).
  • - - Required hyphen separator.
  • (?!0000) - Negative lookahead to reject serial number 0000.
  • \d{4} - Four digits for the serial number (last part).
  • $ - End of the string.

Note: This pattern enforces the standard XXX-XX-XXXX format with hyphens and rejects commonly known invalid patterns. For production use with legacy data or systems that accept SSNs without hyphens, you may need to normalize the input first.

Implementation

const ssnRegex = /^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
const isValidSSN = (ssn) => ssnRegex.test(ssn);

Test Cases

Social Security NumberValid
123-45-6789
234-56-7890
456-78-9012
987-65-4321
800-12-3456
000-12-3456
666-12-3456
900-12-3456
999-12-3456
123-00-4567
123-45-0000
12-34-5678
1234-56-789
123456789
123-456-789
abc-de-fghi
123-45-67890
123.45.6789

Basic Validation

For a quick format check without enforcing SSA rules, you can use a basic regular expression that only validates the XXX-XX-XXXX structure. This is useful when you need to accept the format but will validate the actual number against SSA databases separately.

^\d{3}-\d{2}-\d{4}$

Explanation

  • ^ - Start of the string.
  • \d{3} - Exactly three digits for the area number.
  • - - Required hyphen separator.
  • \d{2} - Exactly two digits for the group number.
  • - - Required hyphen separator.
  • \d{4} - Exactly four digits for the serial number.
  • $ - End of the string.

Implementation

const basicSSNRegex = /^\d{3}-\d{2}-\d{4}$/;
const isValidSSN = (ssn) => basicSSNRegex.test(ssn);

Test Cases

Social Security NumberValid
123-45-6789
234-56-7890
000-12-3456
666-12-3456
900-12-3456
999-12-3456
123-00-4567
123-45-0000
12-34-5678
1234-56-789
123456789
123-456-789
abc-de-fghi
123-45-67890
123.45.6789

Additional Considerations

  • SSN Randomization: Since June 25, 2011, the SSA has assigned SSNs randomly, making it harder to predict valid ranges. The patterns above cover the most common invalid cases.
  • International Variations: This validation is specific to U.S. Social Security Numbers. Other countries have different national identification systems (e.g., Canada's SIN, UK's NINO).
  • Format Flexibility: Some systems accept SSNs without hyphens. Consider normalizing input by removing non-digit characters before validation.
  • ITIN vs SSN: Individual Taxpayer Identification Numbers (ITINs) follow a different format and should not be validated with these patterns.