element ui rules 表单校验(手机号必须为数字,规定长度)

 data() {
    const validateNum = (rule, value, callback) => {
      debugger
      const phone= /^1\d{10}$/    //以1开头的数字
      //const phone= /^[0-9]*$/
      // console.log(phone.test(value))
      // console.log(value)
      if (!(phone.test(value)) && value) {
        callback(new Error('手机号格式不正确'))
      }else{
        callback()
      }
    }
    const validateId = (rule, value, callback) => {
      debugger
      //const phone= /^[0-9]*$/
      const phone= /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      // console.log(phone.test(value))
      // console.log(typeof value)
      if (!(phone.test(value)) && value) {
        callback(new Error('格式不正确'))
      }else{
        callback()
      }
    }
  return { 
    rules: {
        id: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
        phone:[
          { required: false, message: "请输入电话号码", trigger: "blur" },
          { min:7,max:11,message: '长度在7~11个字符',trigger: "blur" },
          // { type: 'number', message: '必须为数字' },
          { validator: validateNum, trigger: "blur"}
        ],
        identityCard:[
          { required: false, message: "请输入身份证号码", trigger: "blur" },
          { min:18,max:18,message: '长度在18个字符',trigger: "blur" },
          // { type: 'number', message: '必须为数字' },
          { validator: validateId, trigger: "blur"}
        ]
      },
  }
}

element ui rules 表单校验(手机号必须为数字,规定长度)_第1张图片

你可能感兴趣的:(javascript,vue.js,elementui)