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.

178 lines
5.4 KiB
JavaScript

2 years ago
const numberReg = /^-?[1-9][0-9]?.?[0-9]*$/
const intReg = /^-?[1-9][0-9]*$/
2 years ago
const phoneReg = /^1[0-9]{10,10}$/
const emailReg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
2 years ago
const pwdReg = /^.{3,20}$/
const inviteCodeReg = /^[a-zA-Z0-9]{4,16}$/
const qqReg = /^[1-9][0-9]{4,14}$/
const fixedPhoneReg = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
const chNameReg = /^[\u4e00-\u9fa5]+$/
const enNameReg = /^([a-zA-Z]+\s)*[a-zA-Z]+$/
2 years ago
const usernameReg = /^[a-zA-Z0-9_.]{4,10}$/;
2 years ago
export default {
isNumber: function(val) {
return numberReg.test(val)
},
isInt: function(val) {
return intReg.test(val)
},
isPhone: function(val) {
return phoneReg.test(val)
},
isEmail: function(val) {
return emailReg.test(val)
},
isPwd: function(val) {
return pwdReg.test(val)
},
isInviteCode: function(val) {
return inviteCodeReg.test(val)
},
validate: function(data, rules) {
2 years ago
let res = { isOk: true, errmsg: '',name: rules.name }
2 years ago
if (!rules || !rules.length) {
return res
}
for (let rule of rules) {
// rule: {name:'', type:'', errmsg:'', min:1, max:2, eq:'', required:Boolean, regex:''}
if (!rule || !rule.name || !rule.type) {
continue
}
// 如果值不存在
if (!data[rule.name]) {
// 如果是必填项就返回错误提示required可以作为type是为了不同的type能给用户不同的提示
if (rule.type === 'required' || rule.required) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
if (!res.errmsg) {
res.errmsg = '请正确输入所有数据' //默认提示
}
return res
}
// 如果不是必填项就跳过
continue
}
switch (rule.type) {
// required 上面已经判断过了
case 'equals':
case 'eq':
if (data[rule.name] !== data[rule.eqName]) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'number':
if (!numberReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'int':
if (!intReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'phone':
if (!phoneReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'email':
if (!emailReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg,name: rule.name }
2 years ago
}
break
case 'pwd':
if (!pwdReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'inviteCode':
if (!inviteCodeReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'range': // 数字类型的值取值范围
// {name: 'xxx', type: 'range', min: 6, max: 6, required: true, errmsg: 'xxx'}
let val = data[rule.name]
if (val) {
if (numberReg.test(val)) {
if (rule.min && val < rule.min) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
} else if (rule.max && val > rule.max) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
} else {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
}
break
case 'lengthRange': // 字符串长度取值范围
// {name: 'xxx', type: 'lengthRange', min: 6, max: 6, errmsg: 'xxx'}
let le = data[rule.name] ? data[rule.name].length : 0
if (rule.min && le < rule.min) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
} else if (rule.max && le > rule.max) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
2 years ago
case 'cfmPwd':
if (!pwdReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}else if(data[rule.oname] !== data[rule.name]){
2 years ago
res = { isOk: false, errmsg: rule.errmsg2 ,name: rule.name }
2 years ago
}
break
2 years ago
case 'regex': // 自定义正则表达式
// {name: 'xxx', type: 'regex', regex: /^1[0-9]{10,10}$/, errmsg: 'xxx'}
if (rule.regex && !rule.regex.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
2 years ago
case 'hasStr':
if (!data[rule.name]) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'agreement':
if (!data[rule.name]) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
2 years ago
}
break
case 'qq':
if (!qqReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
}
break
case 'fixedPhone':
if (!fixedPhoneReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
}
break
case 'chName':
if (!chNameReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
}
break
case 'enName':
if (!enNameReg.test(data[rule.name])) {
2 years ago
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
}
break
case 'username':
if (!usernameReg.test(data[rule.name])) {
res = { isOk: false, errmsg: rule.errmsg ,name: rule.name }
}
break
2 years ago
}
// 发现任何一个错误就立即返回,后面的不再判断
if (!res.isOk) {
if (!res.errmsg) {
res.errmsg = '请正确输入所有数据' //默认提示
}
return res
}
}
return res
}
}