Regular Expression for Validating a Social Security Number (SSN) issued after June 25ᵗʰ, 2011

As of June 25ᵗʰ, 2011 the Social Security Administration has changed how SSNs are assigned, and now the regular expressions I’ve traditionally used to validate them are no longer useful. The super-simplistic “is it 9 digits” kind of validation will still work fine, but I’ve always preferred expressions that enforce more of the old SSN structure rules (which were somewhat complex).

Unfortunately, I couldn’t easily locate a good expression online for the new format. The ones I kept finding were either too-simplistic, or just plain flawed (which is often the case with RegEx patterns you find online).

The official article on SSN Randomization from the Social Security Administration describes a structure that will be much simpler than the old one. It seems that the only remaining restrictions are that the areas 666, 000, and 900-999 remain off-limits.

If you dig deeper into the official FAQ there is also the rule that no part (area, group, or serial) will contain all zeros. I couldn’t find any other rules though, so a lot of previously restricted numbers will now be available for assignment.

I also wanted my expression to allow, optionally, for the use of a single dash or space separator character between each group (allowing for mix-n-match separators, which you do see in some systems).

With that set of rules, I come up with the following regular expression for validation.

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

I ran it against 200k real SSNs, and it validated them all correctly. This pattern should also remain viable for a while, at least until the SSA decides to start giving out the 900-999 area.

2 Replies to “Regular Expression for Validating a Social Security Number (SSN) issued after June 25ᵗʰ, 2011”

    1. My web site apparently removed the backslashes from the regex pattern published here. I’ve put the slashes back in now, so give it another try.

Leave a Reply to Ray Cancel reply

Your email address will not be published. Required fields are marked *