Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
feat: add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTL-UwU committed Dec 15, 2023
1 parent 1ee1a18 commit 83f6214
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/utils/checkId.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
export function checkIds(code: string) {
const city = [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];
const idCardReg = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
let errorMessage = '';
let isPass = true;

if (!code) {
errorMessage = '请输入身份证号码';
isPass = false;
} else if (!code.match(idCardReg)) {
errorMessage = '请输入正确的身份证号码';
isPass = false;
} else if (!city.includes(Number.parseInt(code.substring(0, 2)))) {
errorMessage = '请输入正确的身份证号码';
isPass = false;
} else if (code.length === 18) {
const codeDigits = code.split('');
Expand All @@ -26,13 +22,24 @@ export function checkIds(code: string) {
sum += ai * wi;
}
const last = parity[sum % 11];
if (last.toString() !== codeDigits[17]) {
errorMessage = '请输入正确的身份证号码';
if (last.toString() !== codeDigits[17])
isPass = false;
}
}

if (!isPass) {
const regList = [
// /^[a-zA-Z0-9]{5,17}$/,
/^8[123]0000(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dX]$/, // 港澳台居民居住证
/^[H|h|M|m](\d{8}|\d{10})$/, // 港澳居民来往内地通行证
/^\d{8}|^[a-zA-Z0-9]{10}|^\d{18}$/, // 台湾居民来往大陆通行证
];

for (const reg of regList)
isPass = isPass || reg.test(code);
}

return {
res: isPass,
msg: errorMessage,
msg: '请输入正确的证件号码',
};
}

0 comments on commit 83f6214

Please sign in to comment.