1、使用正则表达式校验身份证格式是否正确
2、计算身份证号最后一位校验码是否正确
function validateIDCard(idCard) {
const pattern = /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}([0-9Xx])$/;
if (!pattern.test(idCard)) {
return false;
}
const weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
let count = 0
for (let i = 0; i < chars.length - 1; i++) {
count += parseInt(chars[i]) * weight[i]
}
const valid = {
"0": "1",
"1": "0",
"2": "X",
"3": "9",
"4": "8",
"5": "7",
"6": "6",
"7": "5",
"8": "4",
"9": "3",
"10": "2"
};
return chars[17] === valid[count % 11]
}