J.H*_*rix 36 algorithm validation
那里的任何人都知道如何改进这个功能?我并不担心缩短代码,我相信这可以通过更好的正则表达式完成,我更关心正确的逻辑.我很难找到SSN#的文档.我在下面使用的大多数规则来自在信用行业工作的其他程序员(没有引用的来源).
感谢您的任何见解!
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;
}
Run Code Online (Sandbox Code Playgroud)
Eri*_* J. 26
UPDATE
2011年6月25日,SSA将SSN分配过程改为"SSN随机化".[27] SSN随机化通过以下方式影响SSN分配过程:
它消除了SSN的前三个数字的地理重要性,以前称为区号,不再分配区号以分配给特定州的个人.它消除了最高组号的重要性,因此,高组列表被及时冻结,并可用于验证在随机化实施日期之前发布的SSN.以前未分配的区号已经引入,不包括区号000,666和900-999.
新规则
http://en.wikipedia.org/wiki/Social_Security_number#Structure
上一个答案
小智 18
截至2011年,SSN完全随机化(http://www.socialsecurity.gov/employer/randomization.html)
剩下的唯一真正的规则是:
MaK*_*aKR 15
由于社会保障管理局对验证规则的更改,在初始问题后5年回答.此外,根据此链接还有特定号码无效.
根据我近2年前的答案,我也省略了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'
);
}
Run Code Online (Sandbox Code Playgroud)
根据更新的信息,没有其他检查要执行.
Zed*_*Zed 11
这一切都在socialsecurity.gov:编号方案,分配,每月更新的最高数字.