美文网首页
ant design vue-1.6.2-----表单验证规则封

ant design vue-1.6.2-----表单验证规则封

作者: 懒懒猫 | 来源:发表于2022-05-11 17:00 被阅读0次

手机号,邮箱,身份证,密码,机构账号

const mobileTest = /^1[3456789]\d{9}$/
const passwordTest = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
const emailTest = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
const identifyTest = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
const agencyCodeTest = /^([A-Z]{2})|([0-9]{2})|([A-Z][0-9])|([0-9][A-Z])/

const handlePasswordCheck = (rule, value, callback) => {
  if (value === undefined || !passwordTest.test(value)) {
    callback(new Error('请输入8到16位密码,包含数字和字母'))
    callback(new Error('请输入8到16位密码,包含数字和字母'))
  }
  callback()
}
const handleUsernameCheck = (rule, value, callback) => {
  if (value === undefined || !(mobileTest.test(value) || emailTest.test(value))) {
    callback(new Error('请输入正确的账号格式'))
  }
  callback()
}
const agencyCodeNumCheck = (rule, value, callback) => {
  if (!value) {
    callback(new Error('机构代码不能为空'))
  } else if (value.length > 2) {
    callback(new Error('机构代码长度不能大于2位'))
  } else if (!agencyCodeTest.test(value)) {
    callback(new Error('请输入正确的格式'))
  } else {
    callback()
  }
}
export const formRulesValidate = {
  mobile: {
    rules: [{ required: true, message: '请输入11位手机号码', pattern: mobileTest }],
    trigger: ['change', 'blur']
  },
  username: {
    rules: [{ required: true, message: '', validator: handleUsernameCheck }],
    trigger: ['change', 'blur']
  },
  password: {
    rules: [{ required: true, message: '', validator: handlePasswordCheck }],
    validateTrigger: ['change', 'blur']
  },
  email: {
    rules: [{ required: true, message: '请输入正确的邮箱格式', pattern: emailTest }],
    trigger: ['change', 'blur']
  },
  identify: {
    rules: [{ required: true, message: '请输入正确的证件号码', pattern: identifyTest }],
    trigger: ['change', 'blur']
  },
  agencyCode: {
    rules: [{ required: true, message: '', validator: agencyCodeNumCheck }],
    trigger: ['change', 'blur']
  }
}

export const noticeTitleCheck = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入标题名称'))
  } else if (value.length > 50) {
    callback(new Error('标题名称字数不能大于50'))
  } else {
    callback()
  }
}

相关文章

网友评论

      本文标题:ant design vue-1.6.2-----表单验证规则封

      本文链接:https://www.haomeiwen.com/subject/xwzdurtx.html