You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// utils/validator.js
// 验证中文姓名2-10个汉字
export const validateChineseName = (name) => {
if (!name) {
return { valid: false, message: '姓名不能为空' };
}
const reg = /^[\u4e00-\u9fa5]{2,10}$/;
if (!reg.test(name)) {
return { valid: false, message: '请输入2-10个汉字' };
}
return { valid: true, message: '' };
};
// 验证手机号码
export const validatePhoneNumber = (phone) => {
if (!phone) {
return { valid: false, message: '手机号码不能为空' };
}
const reg = /^1[3-9]\d{9}$/;
if (!reg.test(phone)) {
return { valid: false, message: '请输入正确的手机号码' };
}
return { valid: true, message: '' };
};
// 验证身份证号码
export const validateIDCard = (idCard) => {
if (!idCard) {
return { valid: false, message: '身份证号码不能为空' };
}
// 基本格式验证
const reg = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
if (!reg.test(idCard)) {
return { valid: false, message: '身份证格式不正确' };
}
// 校验码验证
const idCardArray = idCard.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;
for (let i = 0; i < 17; i++) {
sum += parseInt(idCardArray[i]) * factor[i];
}
const mod = sum % 11;
if (parity[mod] !== idCardArray[17].toUpperCase()) {
return { valid: false, message: '身份证校验码错误' };
}
return { valid: true, message: '' };
};