vue iview怎么验证身份证正则

冰霜
  • 358

customer: {

    name: "",
    idCard: "",
    mobile: ""
  },
  ruleValidateX: {
    name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    idCard: [{ required: true, message: "身份证不能为空", trigger: "blur" }],
    mobile: [{ required: true, message: "手机号不能为空", trigger: "blur" }]
  },
  

// 需要做到 1 .当没有输入内容的时候提示 (身份证不能为空)

          2 .身份证格式错误 时候显示(格式错误)
            (^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)
            
            
            
         -----------下面不起效果---------
          const idCardPass = (rule, value, callback) => {
  let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
  if (value === '') {
     callback(new Error('身份证号不能为空'));
  }else if(!reg.test(value)){
     callback(new Error('请输入正确的身份证号'));
  }
};
// 身份证验证---
return {
  customer: {
    name: "",
    idCard: "",
    mobile: ""
  },
  ruleValidateX: {
    name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    idCard: [{ required: idCardPass, trigger: "blur" }],
    mobile: [{ required: true, message: "手机号不能为空", trigger: "blur" }]
  },
回复
阅读 7.3k
1 个回答

搞定了 是我写错了

idCard: [{ validator: idCardPass, trigger: "blur" }],

宣传栏