我如何验证美国社会安全号码?

问题描述 投票:33回答:8

那里的任何人都知道如何改进这个功能?我并不担心缩短代码,我相信这可以通过更好的正则表达式完成,我更关心正确的逻辑。我很难找到SSN#的文档。我在下面使用的大多数规则来自在信用行业工作的其他程序员(没有引用的来源)。

  1. 您是否知道其他任何规则?
  2. 你知道这有什么不对吗?
  3. 你能引用你的消息来源吗?

感谢您的任何见解!

    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;
    }
algorithm validation
8个回答
23
投票

UPDATE

2011年6月25日,SSA将SSN分配过程改为“SSN随机化”。[27] SSN随机化通过以下方式影响SSN分配过程:

它消除了SSN的前三个数字的地理重要性,以前称为区号,不再为特定状态的个人分配区号。它消除了最高组号的重要性,因此,高组列表被及时冻结,并可用于验证在随机化实施日期之前发布的SSN。以前未分配的区号已经引入,不包括区号000,666和900-999。

新规则

  • 社会安全号码是“AAA-GG-SSSS”格式的九位数字。这个数字分为三个部分。
  • 中间两位数字是组号。组号范围从01到99。
  • 最后四位数是序列号。它们代表组内从0001到9999的直数数字序列。
  • 永远不会分配一些特殊号码: 任何数字组中全零的数字(000 - ## - ####,### - 00 - ####,### - ## - 0000)。 第一个数字组中包含666或900-999(个人纳税人识别号码)的数字。
  • SSNs used in advertising使这些数字无效。

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

上一个答案

这是我找到的SSN化妆的most-complete description


16
投票

截至2011年,SSN完全随机化(http://www.socialsecurity.gov/employer/randomization.html

剩下的唯一真正的规则是:

  • 不能从900-999开始(虽然个人纳税人识别号码,在某些情况下可以像临时居民和无证件/ DACA / DAPA移民一样使用SSN,但格式相同,并以9开头)
  • 不能以666开头
  • 不能以000开头
  • 必须是9位数字或11位用短划线
  • 不能是任何已知的假货; “078051120” - Woolworth钱包Fiasco “219099999” - 被社会安全局用于广告中
  • 许多人也不包括重复序列号码,虽然这些号码现在在技术上是有效的,我为那些被分配了这些号码的可怜的笨蛋感到遗憾。

14
投票

由于Social Security Administration验证规则的变化,在初始问题后5年回答。根据这个link,还有特定的数字无效。

根据我近两年前的回答,我也省略了isNumeric(ssn),因为该字段是一个数字,并且在调用validate函数之前已经剥离了字符。

// 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'
  );
}

根据更新的信息,没有其他检查要执行。


11
投票

这一切都在socialsecurity.govNumbering schemeallocationshighest numbers每月更新。


2
投票

这显然是一个老帖子,但我发现了一些缩短它的方法。根据此链接还有一些特定的数字无效:http://www.snopes.com/business/taxes/woolworth.asp

这就是我做到的。我可以使用正则表达式来重复数字,但是使用特定的数字来使我们无效,我们也可以将五个数据添加到该列表中(由于区号验证,超过5将无效)。我还遗漏了isNumeric(ssn),因为该字段是一个数字,并且在调用validate函数之前已经剥离了字符。

function validateSSN(ssn) {
    // validate format (no all zeroes, length 9
    if (!ssn.match(/^[1-9][0-9]{2}[1-9][0-9]{1}[1-9][0-9]{3}/)
            || ssn.length!=9) return false;

    // validate area number (1st 3 digits)
    var area=parseInt(ssn.substring(0, 3));
    //  standard      railroad numbers (pre-1963)
    if (area>649 && !(area>=700 && area<=728)) return false;

    // disallow specific invalid number
    if (ssn=='078051120' || // fun fact: some idiot boss put his
                            // secretary's ssn in wallets he sold,
                            // now this is 40000 people's ssn
        ssn=='219099999' || // was used in an ad by the Social Security
                            // Administration
        ssn=='123456789' || // although valid it's not yet assigned and
                            // you're not likely to meet the person who
                            // will get it
        ssn=='123121234' || // probably is assigned to someone but more
                            // likely to find someone trying to fake a
                            // number (next is same)
        ssn=='321214321' || // all the rest are likely potentially
                            // valid, but most likely these numbers are
                            // abused
        ssn=='111111111' ||
        ssn=='222222222' ||
        ssn=='333333333' ||
        ssn=='444444444' ||
        ssn=='555555555') return false;

    return true;
}

1
投票

截至911之后社会安全号码的随机化,900系列甚至666中的条目现在可能是有效数字。

目前唯一确定的事情似乎是: 第一组3将永远不会是000 中间组对永远不会是00 而最后四个永远不会是0000

您可以通过首先测试来执行一些测试,以确保条目的数值> = 1010001 [和<1000000000](001-01-0001的ssan似乎是合法分配的最低值)。然后你可以在第4和第5位检查00,在最后4位检查0000。


1
投票

我知道这是一个老问题,但是为了寻找答案的其他人,我想我会添加一个快速的javascript函数来检查给定的SSN是否有效。

function checkSSN() {
    var inputSSN = #YourInput#,
        ssnRegex = new RegExp("^(9[0-9][0-9]|666|000|078051120|219099999|123456789|123121234|321214321)|^([0-8][0-9][0-9]00)|^([0-8][0-9][0-9][0-9][0-9]000)$"),
        repeats = /^(.)\1+$/;

    //make sure we have 2 dashes in the input Social Security number
    if( inputSSN.match(/./g).length === 2) {
        //Once we have confirmed that there are the right number of dashes, remove them, and make sure that the resulting string is a number (you may or may not need this logic depending on the format of your input SSN.
        inputSSN = inputSSN.replace(/-/g, "");

        if(!isNaN(inputSSN)) {
            //Test the input SSN against our regex to ensure that it doesn't contain any disqualifying combinations.
            if(!ssnRegex.test(inputSSN)) {
                //Make sure the input SSN isn't just a repeated number
                if(!repeats.test(inputSSN)) {
                    //If it lands inside of this, we know it's a valid option for a social security number.
                }
        }   
    }
}

对于ssnRegex逻辑:

如果SSN以900-999,666,000或者上面提到的已知不合格SSN之一开始,则第一部分处理。

^(9[0-9][0-9]|666|000|078051120|219099999|123456789|123121234|321214321)

第二部分确保2位数部分不是00

^([0-8][0-9][0-9]00)

第三部分确保最后一部分不是0000

^([0-8][0-9][0-9][0-9][0-9]0000)

我们另外检查以确保他们输入了一个号码,并且他们不只是使用重复的号码。


0
投票

这是我的PHP版本

/**
 * Validate SSN - must be in format AAA-GG-SSSS or AAAGGSSSS
 *
 * @param $ssn
 * @return bool
 */
function validate_ssn($ssn) {

    $ssnTrimmed = trim($ssn);

    // Must be in format AAA-GG-SSSS or AAAGGSSSS
    if ( ! preg_match("/^([0-9]{9}|[0-9]{3}-[0-9]{2}-[0-9]{4})$/", $ssnTrimmed)) {
        return false;
    }

    // Split groups into an array
    $ssnFormatted = (strlen($ssnTrimmed) == 9) ? preg_replace("/^([0-9]{3})([0-9]{2})([0-9]{4})$/", "$1-$2-$3", $ssnTrimmed) : $ssnTrimmed;
    $ssn_array = explode('-', $ssnFormatted);

    // number groups must follow these rules:
    // * no single group can have all 0's
    // * first group cannot be 666, 900-999
    // * second group must be 01-99
    // * third group must be 0001-9999

    foreach ($ssn_array as $group) {
        if ($group == 0) {
            return false;
        }
    }

    if ($ssn_array[0] == 666 || $ssn_array[0] > 899) {
        return false;
    }

    return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.