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!
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
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
(?!000|666|9\d\d) | Negative lookahead | First 3 digits cannot be 000, 666, or 9xx |
\d{3} | Area number | Exactly 3 digits |
- | Separator | Literal hyphen |
(?!00) | Negative lookahead | Group number cannot be 00 |
\d{2} | Group number | Exactly 2 digits |
- | Separator | Literal hyphen |
(?!0000) | Negative lookahead | Serial number cannot be 0000 |
\d{4} | Serial number | Exactly 4 digits |
$ | End anchor | Ensures match to string end |
Detailed Breakdown
(?!000|666|9\d\d)- Negative lookahead for area number:000- Invalid area number666- Invalid area number9\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-6789001-23-4567(area 001 is valid)555-12-3456123-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
- Format only: Validates format and invalid ranges, not actual SSN validity
- No database check: Doesn't verify if SSN is actually issued
- No historical validation: Doesn't check against historical invalid ranges
- Security: SSNs are sensitive PII - handle with extreme care
- 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!