验证邮箱的正则表达式:
或
验证手机号的正则表达式:
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)?(1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{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)?(1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/, message: '请输入合法的手机号', trigger: 'blur' },]}}}
其他正则校验可参考这个
js 正则表达式校验各种类型数字、邮箱、手机号..._Naive_Jam的博客-CSDN博客
本文链接:https://my.lmcjl.com/post/15422.html
展开阅读全文
4 评论