Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

regex - 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)11111111");
    
        //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;
    }
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...