Vue 的身份证 手机号码 邮箱等校验

rules: {
	username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
	realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
	password: [{ required: true, message: "请输入密码", trigger: "blur" }],
	mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { validator: function(rule, value, callback) {
              if (/^1[34578]\d{9}$/.test(value) == false) {
                callback(new Error("手机号格式错误"));
              } else { callback(); }
            },
            trigger: "blur"
          }
        ],
	email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: function(rule, value, callback) {
              if ( /^\w{1,64}@[a-z0-9\-]{1,256}(\.[a-z]{2,6}){1,2}$/i.test( value ) == false ) {
                callback(new Error("邮箱格式错误"));
              } else { callback(); }
            },
            trigger: "blur"
          }
        ],
zjhm: [
          { required: true, message: '请输入身份证', trigger: 'blur' },
          {
            pattern: /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/,
            message: '请输入正确的证件号', trigger: 'blur'
          }
        ]
}

html,其中ref的参数必须和model的参数名称一致

js 部分

this.$refs['formYq'].validate(valid => {
	if (valid) {
            
    } else { return false }
})

 

你可能感兴趣的:(vue)