Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate US Social Security Number?

Anyone out there know how to improve this function? I'm not worried about shortening the code, I'm sure this could be done with better regex, I am more concerned about correct logic. I have had a terrible time finding documentation for SSN #'s. Most of the rules I use below have come from other programmers who work in the credit industry (no sources cited).

  1. Are there any additional rules that you are aware of?

  2. Do you know if any of this is wrong?

  3. Can you site your sources?

    public static bool isSSN(string ssn) {     Regex rxBadSSN = new Regex(@"(\d)\1\1\1\1\1\1\1\1");      //Must be 9 bytes     if(ssn.Trim().Length != 9)         return false;      //Must be numeric     if(!isNumeric(ssn))         return false;      //Must be less than 772999999     if( (Int32)Double.Parse(ssn.Substring(0,3)) > 772 )     {         //Check for Green Card Temp SSN holders         // Could be 900700000         //          900800000         if(ssn.Substring(0,1) != "9")             return false;          if(ssn.Substring(3,1) != "7" && ssn.Substring(3,1) != "8")             return false;     }      //Obviously Fake!     if(ssn == "123456789")         return false;      //Try again!     if(ssn == "123121234")         return false;      //No single group can have all zeros     if(ssn.Substring(0,3) == "000")         return false;     if(ssn.Substring(3,2) == "00")         return false;     if(ssn.Substring(5,4) == "0000")         return false;      //Check to make sure the SSN number is not repeating     if (rxBadSSN.IsMatch(ssn))         return false;      return true; } 
like image 329
J.Hendrix Avatar asked Oct 04 '09 18:10

J.Hendrix


People also ask

Is there a way to validate SSN?

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.

What is a valid SSN format?

Structure. The Social Security number is a nine-digit number in the format "AAA-GG-SSSS".


2 Answers

UPDATE

On June 25, 2011, the SSA changed the SSN assignment process to "SSN randomization".[27] SSN randomization affects the SSN assignment process in the following ways:

It eliminates the geographical significance of the first three digits of the SSN, previously referred to as the Area Number, by no longer allocating the Area Numbers for assignment to individuals in specific states. It eliminates the significance of the highest Group Number and, as a result, the High Group List is frozen in time and can be used for validation of SSNs issued prior to the randomization implementation date. Previously unassigned Area Numbers have been introduced for assignment excluding Area Numbers 000, 666 and 900–999.

New Rules

  • The Social Security number is a nine-digit number in the format "AAA-GG-SSSS". The number is divided into three parts.
  • The middle two digits are the Group Number. The Group Numbers range from 01 to 99.
  • The last four digits are Serial Numbers. They represent a straight numerical sequence of digits from 0001 to 9999 within the group.
  • Some special numbers are never allocated:
    • Numbers with all zeros in any digit group (000-##-####, ###-00-####, ###-##-0000).
    • Numbers with 666 or 900-999 (Individual Taxpayer Identification Number) in the first digit group.
  • SSNs used in advertising have rendered those numbers invalid.

http://en.wikipedia.org/wiki/Social_Security_number#Structure

Previous Answer

Here's the most-complete description of the makeup of an SSN that I have found.

like image 71
Eric J. Avatar answered Sep 22 '22 06:09

Eric J.


As of 2011 SSN's are completely randomized (http://www.socialsecurity.gov/employer/randomization.html)

The only real rules left are:

  • Cannot start with 900-999 (although the Individual Taxpayer Identification Number, which can be used like an SSN by temporary residents and undocumented/DACA/DAPA immigrants in some situations, is in the same format and does start with 9)
  • Cannot start with 666
  • Cannot start with 000
  • Must be 9 numeric digits or 11 with the 2 dashes
  • Cannot be any of the known fakes;
    • "078051120" — Woolworth Wallet Fiasco
    • "219099999" — Was used in an ad by the Social Security Administration
  • Many people exclude repeating an sequential numbers as well, although these are now technically valid, and I feel sorry for the poor schmuck's who gets assigned these.
like image 41
Louis Avatar answered Sep 22 '22 06:09

Louis