function isIdCard(code: string) {
if (code.length !== 18) return false;
const reg = /^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)\d{15}[0-9xX]$/;
if (!reg.test(code)) return false;
const sfz: string[] = code.split('');
const jym: string[] = '10X98765432'.split(''); // 校验码
const org: number[] = sfz.slice(0, 17).map(Number); // 身份证前17位数组
const rat: number[] = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; // 相乘系数
let num = 0;
for (let i = 0; i < rat.length; i++) {
num += org[i] * rat[i];
}
return sfz[17].toUpperCase() === jym[num % 11]; // 身份证最后位 与 校验码匹配
}
console.log(isIdCard("432831196411150810"));