身份证简单校验

身份证前六位为区号, 中间八位为出生日期, 再后三位为顺序码, 偶数分配给女性, 奇数给男性, 最后一位为校验位, 值为身份证前十七位 加权求和 然后对11取模 进行映射

权值为 [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]

映射为 ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"]

示例代码(JS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function verify(idCardNumber) {
const REG = /\d{17}[\dX]/;
const W = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const V = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
const C0 = "0".charCodeAt(0);

if (!REG.test(idCardNumber)) {
return false;
}

let sum = 0;
for (let i = 0; i < idCardNumber.length - 1; i++) {
sum += ((idCardNumber.charCodeAt(i) - C0) * W[i]);
}

let v = V[sum % 11];
return idCardNumber.charAt(idCardNumber.length - 1) == v;
}
 欢迎关注我的博客公众号
如果觉得我的文章对你有用,请随意赞赏
0%