174 lines
4.6 KiB
JavaScript
174 lines
4.6 KiB
JavaScript
// 定义正则表达式
|
|
const regexes = {
|
|
email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/,
|
|
phone: /^(?:(?:\+|00)86)?1\d{10}$/,
|
|
url: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
|
|
ip: /^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$/,
|
|
uuid: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/,
|
|
required: /^.+$/,
|
|
is_number: /^\d+(\.\d+)?$/,
|
|
is_chinese: /^[\u4e00-\u9fa5]+$/,
|
|
is_realname: /^(?:[\u4e00-\u9fa5·]{2,50})$/,
|
|
is_english: /^[A-Za-z]+$/,
|
|
is_id_card: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
|
|
is_bank_card: /^[1-9]\d{9,29}$/,
|
|
is_car_id: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/,
|
|
is_username: /^.{5,18}$/,
|
|
is_password: /^.{4,18}$/,
|
|
is_email: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
|
|
};
|
|
|
|
// 邮箱
|
|
export const checkEmail = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_usis_emailername
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('邮箱格式不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
|
|
// 用户名
|
|
export const checkUserName = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_username
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('用户名格式不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
// 密码
|
|
export const checkPassword = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_password
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('密码格式不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
|
|
// 车牌号
|
|
export const checkCarId = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_car_id
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('车牌号格式不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
// 银行卡
|
|
export const checkBankCard = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_bank_card
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('银行卡号格式不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
|
|
// 验证手机号
|
|
export const checkPhone = (rule, sId, callback) => {
|
|
let _idRe = regexes.phone
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('手机号不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
|
|
// 验证姓名
|
|
export const checkName = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_chinese
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('姓名不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
export const checkRealName = (rule, sId, callback) => {
|
|
let _idRe = regexes.is_realname
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('姓名不正确'));
|
|
}
|
|
callback();
|
|
}
|
|
|
|
// 验证身份证号
|
|
export const checkIdCard = (rule, sId, callback) => {
|
|
if (!sId) {
|
|
return callback(new Error('身份证号不能为空'));
|
|
}
|
|
let _idRe =
|
|
/^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/
|
|
if (!_idRe.test(sId)) {
|
|
callback(new Error('⾝份证长度或格式错误'));
|
|
}
|
|
//⾝份证城市
|
|
const aCity = {
|
|
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: "国外"
|
|
};
|
|
if (!aCity[parseInt(sId.substr(0, 2))]) {
|
|
callback(new Error('身份证地区有误'));
|
|
}
|
|
// 出生期验证
|
|
const sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2)))
|
|
.replace(/-/g, "/")
|
|
const d = new Date(sBirthday)
|
|
if (sBirthday != (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
|
|
callback(new Error('出生日期有误'));
|
|
}
|
|
// ⾝份证号码校验
|
|
if (sId.length == 18) {
|
|
let sum = 0
|
|
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
|
|
codes = "10X98765432"
|
|
for (let i = 0; i < sId.length - 1; i++) {
|
|
sum += sId[i] * weights[i];
|
|
}
|
|
const last = codes[sum % 11]; //计算出来的最后⼀位⾝份证号码
|
|
if (sId[sId.length - 1] != last) {
|
|
callback(new Error('身份证号校验码有误'));
|
|
}
|
|
callback();
|
|
}
|
|
};
|
|
|
|
export const validator = {
|
|
checkName,
|
|
checkIdCard,
|
|
checkPhone,
|
|
checkCarId,
|
|
checkBankCard,
|
|
checkUserName,
|
|
checkPassword,
|
|
checkEmail
|
|
}
|
|
|
|
export default regexes; |