views:

358

answers:

2

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?

Thanks for any insight!

    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;
    }
+5  A: 

It's all at socialsecurity.gov: Numbering scheme, allocations, highest numbers updated monthly.

Zed
+2  A: 

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

Eric J.