1、邮箱正则
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
2、手机正则
const regMobile =/^1[3-9]\d{9}$/
使用
vue2
data() {
// 验证邮箱的规则
var checkEmail = (rule, value, cb) => {
// 验证邮箱的正则表达式
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
if (regEmail.test(value)) {
// 合法邮箱
return cb()
}
cb(new Error('请输入合法的邮箱'))
}
// 验证手机号的规则
var checkMobile = (rule, value, cb) => {
// 验证手机号的正则表达式
const regMobile =/^1[3-9]\d{9}$/
if (regMobile.test(value)) {
// 合法手机号
return cb()
}
cb(new Error('请输入合法的手机号'))
}
return {
// 添加表单的验证规则对象
addFormRules: {
email: [
{ required: true, message: '请输入邮箱', trigger: 'blur' },
{ validator: checkEmail, trigger: 'blur' },
],
mobile: [
{ required: true, message: '请输入手机', trigger: 'blur' },
{ validator: checkMobile, trigger: 'blur' }
]
}
}
}
vue3
var checkEmail = (rule, value, cb) => {
// 验证邮箱的正则表达式
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
if (regEmail.test(value)) {
// 合法邮箱
return cb()
}
cb(new Error('请输入合法的邮箱'))
};
var checkMobile = (rule, value, cb) => {
// 验证手机号的正则表达式
const regMobile =/^1[3-9]\d{9}$/
if (regMobile.test(value)) {
// 合法手机
return cb()
}
cb(new Error('请输入合法的手机'))
};
const loginRules = {
username: [{required: true, trigger: 'blur', message: '请输入用户名'}],
nickName: [{required: true, trigger: 'blur', message: '请输入昵称'}],
phone: [
{required: true, trigger: 'blur', message: '请输入手机号'},
{ validator: checkMobile, trigger: 'blur' },
],
email: [
{required: true, trigger: 'blur', message: '请输入邮箱'},
{ validator: checkEmail, trigger: 'blur' },
],
regionCode: [{required: true, trigger: 'blur', message: '请选择所属地市'}],
roleId: [{required: true, trigger: 'blur', message: '请选择角色'}],
}
网友评论