Last active
November 19, 2015 15:10
-
-
Save consatan/f98ecbad57b9846a4b2b to your computer and use it in GitHub Desktop.
验证所给的身份证号码是否符合中国大陆二代身份证号码规则
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 验证所给的身份证号是否符合中国大陆二代身份证号码规则 | |
* | |
* @param string id 身份证号码 | |
* @return boolean 符合二代身份证号码规则返回 true,否则返回false | |
*/ | |
function checkChinaID(id) { | |
var n = 0, | |
i = 0, | |
a = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; | |
if (id.length !== 18) return false; | |
for (; i < 17;) { | |
n += a[i] * parseInt(id.slice(i++, i), 10); | |
} | |
return id.slice(-1).toUpperCase() === ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'][n % 11]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment