验证邮箱的正则表达式:
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
或
const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
验证手机号的正则表达式:
const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
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 =/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/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' }]}}}
不合法的提示:
合法的:
rules的另一个用法:
pattern
data() {return {// 添加表单的验证规则对象addFormRules: {email: [{ required: true, message: '请输入邮箱', trigger: 'blur' },{ pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/, message: '请输入合法的邮箱', trigger: 'blur' },],mobile: [{ required: true, message: '请输入手机', trigger: 'blur' },{ pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/, message: '请输入合法的手机号', trigger: 'blur' },]}}}