/**
|
* elementUI 表单验证
|
*/
|
const validators = {
|
required: {
|
rule: /.+/,
|
msg: '必填项不能为空'
|
},
|
phone: {
|
rule: /^[1][3,4,5,6,7,8][0-9]{9}$/,
|
msg: '手机号格式不正确'
|
},
|
mail: {
|
rule: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
|
msg: 'E-mail格式不正确'
|
},
|
id_card: {
|
rule: /(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{15}$)/,
|
msg: '身份证格式不正确'
|
},
|
password: {
|
rule: /^\d{6}$/,
|
msg: '密码必须为6位数字'
|
},
|
code: {
|
rule: /^\d{4}$/,
|
msg: '验证码格式不正确'
|
},
|
number: {
|
rule: /^\d+$/,
|
msg: '必须为整数'
|
},
|
digit: {
|
rule: /^\d+(\.\d+)?$/,
|
msg: '必须为数值'
|
},
|
same: {
|
rule (val='', sVal='') {
|
return val===this.data[sVal]
|
},
|
msg: '密码不一致'
|
},
|
// https://blog.csdn.net/xjun0812/article/details/81806118
|
carNo: {
|
// 含新能源车
|
rule: /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/,
|
msg: '车牌号格式不正确'
|
},
|
}
|
|
/**
|
* 验证手机号
|
* @param {string} options.trigger 触发事件
|
*/
|
function validatorTel(options = {}){
|
return [
|
{
|
validator: (rule, value, callback)=>{
|
if (!value) {
|
return callback(new Error('请输入手机号码'))
|
}
|
if (!validators.phone.rule.test(value)) {
|
return callback(new Error('请输入正确格式手机号'))
|
}
|
callback()
|
},
|
trigger: options.trigger || 'blur'
|
}
|
]
|
}
|
|
/**
|
* 验证电子邮箱
|
* @param {string} options.trigger 触发事件
|
*/
|
function validatorEmail(options = {}){
|
return [
|
{
|
validator: (rule, value, callback) => {
|
if (!value) {
|
return callback(new Error('请输入电子邮箱'))
|
}
|
if (!validators.mail.rule.test(value)) {
|
return callback(new Error('请输入正确格式电子邮箱'))
|
}
|
callback()
|
},
|
trigger: options.trigger || 'blur'
|
}
|
]
|
}
|
|
/**
|
* 验证非空
|
* @param {object} options 选项
|
* @param {string} options.message 提示
|
* @param {string} options.trigger 触发事件
|
*/
|
function validatorRequired(options = {}){
|
return [{
|
validator: (rule, value, callback) => {
|
// =='' 可以兼容数组为空的情况
|
if (value=='') {
|
return callback(new Error(options.message || validators.required.msg))
|
}
|
callback()
|
},
|
trigger: options.trigger || 'blur'
|
}]
|
}
|
|
/**
|
* 验证密码
|
* @param {object} options
|
* @param {string} options.message 可选,提示文本
|
* @param {function} options.beforeCallback 可选,用于检查确认密码
|
* @param {string} optionss.trigger 触发事件
|
*/
|
function validatorPassword(options = {}){
|
return [{
|
validator: (rule, value, callback)=>{
|
if (!value) {
|
return callback(new Error(options.message || '请输入密码'))
|
}
|
// 预留,用于检查确认密码
|
typeof options.beforeCallback === 'function' && options.beforeCallback()
|
callback()
|
},
|
trigger: options.trigger || 'blur'
|
}]
|
}
|
|
/**
|
* 验证确认密码
|
* @param {object} options
|
* @param {function} options.password 必须,用于获取密码
|
* @param {string} optionss.trigger 触发事件
|
*/
|
function validatorConfirmPwd(options = {}){
|
return [{
|
validator: (rule, value, callback)=>{
|
// 动态获取密码
|
var password
|
if (typeof options.password === 'function') {
|
password = options.password()
|
} else {
|
password = options.password
|
}
|
|
if (!value) {
|
callback(new Error('请输入确认密码'))
|
} else if (value !== password) {
|
callback (new Error('两次输入密码不一致!'))
|
} else{
|
callback()
|
}
|
},
|
trigger: options.trigger || 'blur'
|
}]
|
}
|
|
export default {
|
validators,
|
validatorTel,
|
validatorEmail,
|
validatorRequired,
|
validatorPassword,
|
validatorConfirmPwd,
|
}
|