Skip to content

Instantly share code, notes, and snippets.

@clifferson
Created August 30, 2025 19:40
Show Gist options
  • Save clifferson/b13511ab22f1bc3b3591cbd3ab97195c to your computer and use it in GitHub Desktop.
Save clifferson/b13511ab22f1bc3b3591cbd3ab97195c to your computer and use it in GitHub Desktop.
⚙️ The SSN Format Validation Algorithm
This is the most you can do without calling an external service. The process checks if a given SSN string adheres to the standard format and avoids known invalid number ranges. The core of this process is often a regular expression (regex) that checks for specific patterns and invalid combinations.
1. Basic Format and Length
A valid SSN must have nine digits. It's most commonly presented in the format AAA-GG-SSSS (Area-Group-Serial).
AAA (Area): The first three digits.
GG (Group): The next two digits.
SSSS (Serial): The final four digits.
The algorithm first checks if the input string matches this structure, allowing for either hyphens or no hyphens.
2. Prohibited Number Ranges
This is the most critical part of the validation beyond simple formatting. An SSN cannot be:
Area Number (AAA):
000
666
Any number in the range 900 to 999
Group Number (GG):
00
Serial Number (SSSS):
0000
The algorithm must check that none of these specific number groups are present in their corresponding positions.
3. Known Invalid Numbers
Certain specific SSN numbers are known to be invalid due to historical reasons, such as the "Woolworth's Wallet Fiasco" or being used in public advertisements. A more robust validation algorithm would include these specific numbers in its blocklist. Examples include 078-05-1120 and 219-09-9999.
4. Implementation Using Regular Expressions
A regular expression is the most efficient way to implement the above rules without writing extensive procedural code. For an SSN in the format AAA-GG-SSSS, a comprehensive regex would look something like this:
^(?!000|666|9\d{2})\d{3}-(?!00)\d{2}-(?!0000)\d{4}$
Let's break down this regex:
^ : Asserts the beginning of the string.
(?!000|666|9\d{2}) : This is a negative lookahead that ensures the following three digits are not 000, 666, or in the 900-999 range.
\d{3} : Matches exactly three digits for the Area Number.
- : Matches a literal hyphen.
(?!00) : Another negative lookahead ensuring the following two digits are not 00.
\d{2} : Matches exactly two digits for the Group Number.
- : Matches a literal hyphen.
(?!0{4}) : A negative lookahead for the last four digits, ensuring they are not 0000.
\d{4} : Matches exactly four digits for the Serial Number.
$ : Asserts the end of the string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment