How can I validate US Social Security Number?

后端 未结 8 1460
鱼传尺愫
鱼传尺愫 2020-12-08 02:44

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 cor

相关标签:
8条回答
  • 2020-12-08 03:25

    Answer 5 years after initial question due to changes in validation rules by the Social Security Administration. Also there are Specific numbers to invalidate according to this link.

    As per my near-2-year-old answer I also left out isNumeric(ssn) because the field is a numeric and already strips characters before calling the validate function.

    // validate social security number with ssn parameter as string
    function validateSSN(ssn) {
      // find area number (1st 3 digits, no longer actually signifies area)
      var area = parseInt(ssn.substring(0, 3));
      return (
        // 9 characters
        ssn.length === 9 &&
        // basic regex
        ssn.match(/^[0-8]{1}[0-9]{2}[0-9]{2}[0-9]{4}/) &&
        // disallow Satan's minions from becoming residents of the US
        area !== 666 &&
        // it's not triple nil
        area !== 0 &&
        // fun fact: some idiot boss put his secretary's ssn in wallets
        // he sold, now it "belongs" to 40000 people
        ssn !== '078051120' &&
        // was used in an ad by the Social Security Administration
        ssn !== '219099999'
      );
    }
    

    According to updated information there are no other checks to perform.

    0 讨论(0)
  • 2020-12-08 03:29

    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.

    0 讨论(0)
提交回复
热议问题