如何使用Vuelidate验证密码? [英] How to validate password with Vuelidate?

查看:23
本文介绍了如何使用Vuelidate验证密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要验证密码表单除了需要的字段必须至少有一个大写字母,至少有小写字母,数字至少有一个和至少一个以下字符#?!@$%^&*-"我正在使用这个包 https://vuelidate.js.org/

Greeting, i need to validate the password form In addition to the field required Must have at least one uppercase letter, lowercase letter at least, number at least one and at least one of the following characters "#?! @ $% ^ & * -" I am using this package https://vuelidate.js.org/

编辑

或者为此使用正则表达式

OR REGEX FOR THIS

推荐答案

只需将带有您想要的规则的自定义函数添加到 Vuelidate 验证中即可.

Just add a custom function with the rules you want to the Vuelidate validations.

validations: {
  password: {
    required,
    // minLength: minLength(8)  // I assume you'd want something like this too
    valid: function(value) {
      const containsUppercase = /[A-Z]/.test(value)
      const containsLowercase = /[a-z]/.test(value)
      const containsNumber = /[0-9]/.test(value)
      const containsSpecial = /[#?!@$%^&*-]/.test(value)
      return containsUppercase && containsLowercase && containsNumber && containsSpecial
    }
  }
}

将每个需求分解为单独的函数可能会有所帮助,因此您可以为每个需求设置不同的错误消息(这将有助于指导用户了解他们需要修复的内容).

It'd probably be helpful to break each requirement up into a separate function, so you can set a different error message for each (which would be helpful to guide the user as to what to they need to fix).

validations: {
  password: {
    required,
    // minLength: minLength(8)  // I assume you'd want something like this too
    containsUppercase: function(value) {
      return /[A-Z]/.test(value)
    },
    containsLowercase: function(value) {
      return /[a-z]/.test(value)
    },
    containsNumber: function(value) {
      return /[0-9]/.test(value)
    },
    containsSpecial: function(value) {
      return /[#?!@$%^&*-]/.test(value)
    }
  }
}

这篇关于如何使用Vuelidate验证密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆