Rules for validating social security numbers

Rated 4.42/5 based on 703 customer reviews

^ # Assert position at the beginning of the string. 000|666) # Assert that neither "000" nor "666" can be matched here. (); //Valid SSNs ssns.add("1"); ssns.add("8"); //Invalid SSNs ssns.add("0"); ssns.add("6"); ssns.add("9"); ssns.add("85-345-6789"); ssns.add("856-453-6789"); ssns.add("81"); ssns.add("856-456789"); String regex = "^(?!

It is not intended to be a comprehensive summary of all laws which may be applicable to your situation, treat exhaustively the subjects covered, provide legal advice, or render a legal opinion.

This first part is easily accomplished using a couple of character classes and a quantifier: .

First, we know that we can match any three-digit number starting with 0 through 6, because the preceding negative lookahead already ruled out the invalid numbers 000 and 666.

If you simply need to ensure that a string follows the basic Social Security number format and that obvious, invalid numbers are eliminated, the following regex provides an easy solution. [0-6] # Match a character in the range between "0" and "6".

If you need a more rigorous solution that checks with the Social Security Administration to determine whether the number belongs to a living person, refer to the links in the section of this recipe.^ # Assert position at the beginning of the string. 000|666) # Assert that neither "000" nor "666" can be matched here.

Leave a Reply