Regex for US Social Security number

Use /^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/ to check that a whole string is a valid for US Social Security number. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/
Open in Regex Tester
InputResult
123-45-6789✓ Match
219-09-9999✓ Match
000-12-3456✗ No match
666-12-3456✗ No match
123-00-4567✗ No match
123456789✗ No match
900-12-3456✗ No match

How the pattern works

PartMeaning
(?!000|666|9\d\d)\d{3}Area number: three digits, but never 000, 666 or 900–999
(?!00)\d{2}Group number: never 00
(?!0000)\d{4}Serial number: never 0000
-Dashes between the parts

Code examples

JavaScript

const re = /^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/;
re.test(value); // true or false

Python

import re

bool(re.fullmatch(r'(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}', value))

PHP

preg_match('/^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/', $value) === 1;

Notes and limitations

Negative lookaheads rule out the number ranges the Social Security Administration never issues. Treat SSNs as sensitive data: mask them in logs and interfaces.

Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool

Frequently asked questions

What is the regex for US Social Security number?
/^(?!000|666|9\d\d)\d{3}-(?!00)\d{2}-(?!0000)\d{4}$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: 123-45-6789, 219-09-9999. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.