Regex to Validate a Social Security Number
Here is a regex to validate a Social Security number, and here is the trap most developers fall into: the famous five-second pattern checks the shape and nothing else. 000-00-0000 passes it. That is the number people type to get past a required field, and your "validation" just waved it through.
The shape-only pattern is ^\d{3}-\d{2}-\d{4}$. It confirms three digits, hyphen, two digits, hyphen, four digits. Fine for catching typos, useless for catching the cases that matter, because the Social Security Administration has actual rules about which numbers can exist, and the shape check knows none of them.
A regex to validate a Social Security number has to enforce the SSA rules
A Social Security number is AAA-GG-SSSS: a three-digit area number, a two-digit group number, and a four-digit serial number. The SSA never assigns area numbers of 000 or 666, or anything starting with 9 (the 900 to 999 range). The group number is never 00. The serial number is never 0000. Note that since the 2011 randomization, area numbers are no longer assigned by geography, so do not try to be clever about which area numbers "look real."
The canonical treatment of this problem is recipe 4.12 of the O'Reilly Regular Expressions Cookbook, which has been the reference answer since 2009. Their strict pattern is ^(?!000|666)(?:[0-6][0-9]{2}|7(?:[0-6][0-9]|7[0-2]))-(?!00)[0-9]{2}-(?!0000)[0-9]{4}$, which even bounds the area number range. For most form validation, the more readable lookahead version does the same job with less ceremony:
^(?!666|000|9\d{2})\d{3}[- ]?(?!00)\d{2}[- ]?(?!0{4})\d{4}$
Piece by piece: the (?!...) groups are negative lookaheads, meaning "fail if what follows matches this." (?!666|000|9\d{2}) rejects the invalid area numbers before the three digits are consumed. [- ]? allows a hyphen or space separator, or none. (?!00) rejects the group number 00, (?!0{4}) rejects the all-zero serial, and the anchors ^ and $ make sure the whole string is the number, not part of a longer one.
A quick aside about a detail that trips people up: the lookahead for the area number checks digits that have not been consumed yet, which is why it sits before the \d{3}. If you are new to lookaheads, our lookahead and lookbehind guide walks through the mechanics with simpler examples.
What regex cannot do
No pattern, however strict, can tell you a number was actually issued. The O'Reilly recipe says this explicitly: if you need to know whether the number belongs to a living person, you check with the Social Security Administration. Regex is a typo filter. Treat it as one.
And the security half of the job matters more than the pattern. Validate SSNs server-side too, never trust the client-side check alone, mask them in displays (***-**-6789), and do not log them. A regex that validates perfectly and a log file full of raw SSNs is a failure, not a success.
Frequently asked questions
What is the regex to validate a Social Security number?
The pattern ^(?!666|000|9\d{2})\d{3}[- ]?(?!00)\d{2}[- ]?(?!0{4})\d{4}$ enforces the AAA-GG-SSSS format and the SSA rules: the area number cannot be 000, 666, or start with 9, the group number cannot be 00, and the serial cannot be 0000. The simpler ^\d{3}-\d{2}-\d{4}$ checks shape only and accepts obviously invalid numbers like 000-00-0000.
What are the SSA rules for a valid Social Security number?
The three-digit area number cannot be 000, 666, or in the 900 to 999 range. The two-digit group number cannot be 00. The four-digit serial number cannot be 0000. A regex can enforce these rules but cannot verify that a number was actually issued to a real person.
Can a regex verify that an SSN is real?
No. Regex can only verify format and rule compliance. Verifying that a number belongs to a living person requires checking against Social Security Administration records. Treat client-side regex as a first filter for typos, and do real verification server-side.
Should SSNs be validated on the client side?
Client-side validation is fine for catching typos and bad formats before submission, for example with the HTML pattern attribute. But never rely on it for security: validate again on the server, never log or store SSNs you do not need, and mask them in any display.
Related reading: Regex Lookahead and Lookbehind, Explained · Phone Number Regex vs libphonenumber · Email Regex Validation Mistakes · Username Validation Regex: Best Practice · Regex for Credit Card Number Validation