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

Commit

Permalink
feat: better id check
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTL-UwU committed Dec 15, 2023
1 parent fb111a2 commit 1ee1a18
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
20 changes: 8 additions & 12 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Cover from './components/Shapes/Cover.vue';
import FormItem from './components/FormItem.vue';
import Ticket from './components/Ticket.vue';
import type { TForm, TFormFields } from './types';
import { checkIds } from './utils/checkId';
const form: TForm = reactive({
name: {
Expand All @@ -55,25 +56,20 @@ const form: TForm = reactive({
});
const formRule = {
name: {
reg: /^.{2,15}$/,
msg: '姓名长度应在2~15',
},
id: {
reg: /^([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]$/,
msg: '请输入正确的身份证号',
name: (val: string) => {
return { res: /^.{2,15}$/.test(val), msg: '姓名长度应在2~15' };
},
phone: {
reg: /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
msg: '请输入正确的手机号',
id: checkIds,
phone: (val: string) => {
return { res: /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(val), msg: '请输入正确的手机号' };
},
};
const ticketOpen = ref(false);
function checkRule(field: TFormFields) {
const { reg, msg } = formRule[field];
if (reg.test(form[field].val)) {
const { res, msg } = formRule[field](form[field].val);
if (res) {
form[field].msg = '';
return true;
} else {
Expand Down
38 changes: 38 additions & 0 deletions src/utils/checkId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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('');
const factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
let sum = 0;
let ai = 0;
let wi = 0;
for (let i = 0; i < 17; i++) {
ai = Number.parseInt(codeDigits[i]);
wi = factor[i];
sum += ai * wi;
}
const last = parity[sum % 11];
if (last.toString() !== codeDigits[17]) {
errorMessage = '请输入正确的身份证号码';
isPass = false;
}
}
return {
res: isPass,
msg: errorMessage,
};
}
2 changes: 1 addition & 1 deletion tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
},
"noEmit": true
},
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
"include": ["env.d.ts", "src/**/*", "src/**/*.vue", "src/**/*.ts"],
"exclude": ["src/**/__tests__/*"]
}

0 comments on commit 1ee1a18

Please sign in to comment.