Vuejs:审核条件验证

时间:2020-06-11 18:56:20

标签: vuejs2 vuetify.js vuelidate

假设我们有一个密码字段,并且我们想对长度,特殊字符,数字,大写和小写字母等进行验证,仅在该字段具有值的情况下

我们如何使用vuelidate做到这一点。

我在这里输入vuelidate

import { required, minLength, maxLength, email, sameAs } from 'vuelidate/lib/validators'

在这里验证

validations: {
    editedItem: {
      firstname: { required, minLength: minLength(3), maxLength: maxLength(20) },
      lastname: { required, minLength: minLength(3), maxLength: maxLength(20) },
      email: { required, email },
      password: {
        required,
        minLength: minLength(6),
        maxLength: maxLength(15),
        oneNumber,
        oneUpperCase,
        oneLowerCase,
      },
      repassword: {
        sameAsPassword: sameAs('password'),
      },
    },
  }

oneNumber,oneUpperCase和oneLowerCase是自定义验证:

const oneNumber = (value) => /[0-9]/.test(value)
const oneUpperCase = (value) => /[A-Z]/.test(value)
const oneLowerCase = (value) => /[a-z]/.test(value)

我将永远感谢您的任何帮助或建议

1 个答案:

答案 0 :(得分:2)

我的解决方案涉及一个单独的“自定义验证程序”文件。

validators.js

import { helpers as vuelidateHelpers } from 'vuelidate/lib/validators'

export const oneUppercase = value => {
  if (!vuelidateHelpers.req(value)) {
    return true
  }
  const match = value.match(/[A-Z]/g) || []
  return match.length >= 1
}
export const oneLowercase = value => {
  if (!vuelidateHelpers.req(value)) {
    return true
  }
  const match = value.match(/[a-z]/g) || []
  return match.length >= 1
}
export const oneSpecial = value => {
  if (!vuelidateHelpers.req(value)) {
    return true
  }
  const match = value.match(/[ !@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/g) || []
  return match.length >= 1
}
export const oneNumber = value => {
  if (!vuelidateHelpers.req(value)) {
    return true
  }
  const match = value.match(/\d/g) || []
  return match.length >= 1
}

在组件中,具有可视格式,以使用户知道其密码是否符合要求:

<template>
  <form>
    <label for="password">Password</label>
    <input
            id="password"
            name="password"
            type="password"
            v-model="form.password"
            @blur="$v.form.password.$touch()"
            @input="validatePassword"
    />
    <label for="confirmPassword">Confirm Password</label>
    <input
            id="confirmPassword"
            name="confirmPassword"
            type="password"
            v-model="form.confirmPassword"
            @blur="$v.form.confirmPassword.$touch()"
    />
    <ul>
      <li>
          {{
          passwordValidations.eightCharacters ?
          '✓':
          '╳'
          }}
        <span class="ml-1">MUST contain 8 characters</span>
      </li>
      <li>
          {{
          passwordValidations.oneUppercase ?
          '✓':
          '╳'
          }}
        <span class="ml-1">MUST contain one uppercase letter</span>
      </li>
      <li>
          {{
          passwordValidations.oneLowercase ?
          '✓':
          '╳'
          }}
        <span class="ml-1">MUST contain one lowercase letter</span>
      </li>
      <li>
          {{
          passwordValidations.oneNumber ?
          '✓':
          '╳'
          }}
        <span class="ml-1">MUST contain one number</span>
      </li>
      <li>
          {{
          passwordValidations.oneSpecial ?
          '✓':
          '╳'
          }}
        <span class="ml-1">MUST contain one special character</span>
      </li>
    </ul>
  </form>
</template>

<script>
  import {
    required,
    minLength,
    sameAs,
  } from 'vuelidate/lib/validators'
  import {
    oneNumber,
    oneSpecial,
    oneUppercase,
    oneLowercase,
  } from '../validators'

  export default {
    data () {
      return {
        form: {
          password: '',
          confirmPassword: '',
        },
        passwordValidations: {
          eightCharacters: false,
          oneUppercase: false,
          oneLowercase: false,
          oneNumber: false,
          oneSpecial: false,
        },
      }
    },
    computed: {
      passwordErrors () {
        const errors = []
        if (!this.$v.form.password.$dirty) return errors
        !this.$v.form.password.required && errors.push('Password is required.')
        return errors
      },
      confirmPasswordErrors () {
        const errors = []
        if (!this.$v.form.confirmPassword.$dirty) return errors
        !this.$v.form.confirmPassword.required && errors.push('Please confirm your password.')
        !this.$v.form.confirmPassword.sameAsPassword && errors.push('Passwords don\'t match')
        return errors
      },
    },
    methods: {
      validatePassword () {
        this.passwordValidations.eightCharacters = this.$v.form.password.eightCharacters
        this.passwordValidations.oneUppercase = this.$v.form.password.oneUppercase
        this.passwordValidations.oneLowercase = this.$v.form.password.oneLowercase
        this.passwordValidations.oneNumber = this.$v.form.password.oneNumber
        this.passwordValidations.oneSpecial = this.$v.form.password.oneSpecial
      },
    },
    validations: {
      form: {
        password: {
          required,
          minLength: minLength(8),
          oneUppercase,
          oneSpecial,
          oneNumber,
          oneLowercase,
        },
        confirmPassword: {
          required,
          sameAsPassword: sameAs('password'),
        },
      },
    },
  }
</script>