Regex
regex
December 14, 20223 min read

Regex for SSN validation

SSN is the abbreviation for Social Security Number. It is a nine-digit number that is issued by the Social Security Administration (SSA) to U.S. citizens, permanent residents, and temporary (working) residents. The SSN is used to track an individual's earnings and to determine eligibility for various government programs and benefits. It is also used for identification purposes by employers, banks, and other organizations. In this article let's understand how we can create a regex for SSN and how regex can be matched for a valid SSN.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of a SSN

A SSN should have the following criteria and structure-

  • It should have 9 digits.
  • It should be divided into 3 parts by hyphen (-).
  • The first part should have 3 digits and should not be 000, 666, or between 900 and 999.
  • The second part should have 2 digits and it should be from 01 to 99.
  • The third part should have 4 digits and it should be from 0001 to 9999.

Regex for checking if SSN is valid or not

Regular Expression-

/^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/gm

Test string examples for the above regex-

Input StringMatch Output
000-42-2242does not match
856-45-6789matches
856-452-6789does not match
678-43-1535matches

Here is a detailed explanation of the above regex-

/^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/gm

^ asserts position at start of a line
Negative Lookahead (?!666|000|9\d{2})
Assert that the Regex below does not match
1st Alternative 666
666 matches the characters 666 literally (case sensitive)
2nd Alternative 000
000 matches the characters 000 literally (case sensitive)
3rd Alternative 9\d{2}
9 matches the character 9 with index 5710 (3916 or 718) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
\d matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Negative Lookahead (?!00)
Assert that the Regex below does not match
00 matches the characters 00 literally (case sensitive)
\d matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Negative Lookahead (?!0{4})
Assert that the Regex below does not match
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
{4} matches the previous token exactly 4 times
\d matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if the string is a valid SSN or not.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 13, 20225 min read
Next Article
December 13, 20225 min read