Skip to content

Instantly share code, notes, and snippets.

@consatan
Last active November 19, 2015 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consatan/f98ecbad57b9846a4b2b to your computer and use it in GitHub Desktop.
Save consatan/f98ecbad57b9846a4b2b to your computer and use it in GitHub Desktop.
验证所给的身份证号码是否符合中国大陆二代身份证号码规则
/**
* 验证所给的身份证号是否符合中国大陆二代身份证号码规则
*
* @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