其他分享
首页 > 其他分享> > Vue表单验证

Vue表单验证

作者:互联网



      // 表单 规则
      rules: { 
        CertNo: [{ required: true, validator: validateCertNo, trigger: 'change' }], 
        Phone: [{ required: true, validator: validatePhone, trigger: 'change' }], 
        ExpertsType: [{ required: true, validator: validateExpertsType, trigger: 'change' }], 
      },

    //验证数组类型
    const validateExpertsType = (rule, value, callback) => {
      if (value.length <= 0) {
        callback('请选择类型')
      } else {
        callback()
      }
    }
    //验证手机号
    const validatePhone = (rule, value, callback) => {
      if (value == '' || value == undefined || value == null || value.length == 0) {
        callback('手机号不能为空')
      } else {
        if (!/^1[3456789]\d{9}$/.test(value)) {
          callback('手机号格式不正确')
        } else {
          callback()
        }
      }
    }
    //验证身份证
    const validateCertNo = (rule, value, callback) => {
      if (value.length <= 0) {
        callback('请输入身份证号码')
      } else {
        const reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/
        if (!reg.test(value)) {
          callback('身份证号格式错误')
        }else{ 
          callback()
        }
      }
    }
 
 

 

标签:Vue,验证,required,value,表单,callback,trigger,true,change
来源: https://www.cnblogs.com/Jing213/p/16473240.html