【问题标题】:How to do Password validation in react js using joi schema?如何使用 joi 模式在反应 js 中进行密码验证?
【发布时间】:2021-05-22 01:37:06
【问题描述】:

我想验证密码规则: 密码长度为 8 个字符,应包含 1 个大写和 1 个小写,1 个数字和 1 个特殊字符。

password: new passwordComplexity({
      min: 8,
      max: 25,
      lowerCase: 1,
      upperCase: 1,
      numeric: 1,
      symbol: 1,
    }),
password: Joi.string()
  .min(8)
  .max(25)
  .required()
  .label("Password")
  .error((errors) => {
    errors.forEach((err) => {
      switch (err.type) {
        case "any.empty":
          err.message = passwordRequired;
          break;

        case "string.min":
          err.message = passwordInvalid;
          break;

        case "string.max":
          err.message = passwordInvalidMax;
          break;

        default:
      }
    });
    return errors;
  }),

我尝试了这些,但我猜 passwordComplexity 仅适用于节点 js。有什么帮助吗?

【问题讨论】:

    标签: reactjs validation schema joi


    【解决方案1】:

    你可以尝试使用正则表达式并传入模式

    Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$'))
    

    有关如何使用正则表达式的更多信息:https://regexone.com/

    【讨论】:

      【解决方案2】:

      感谢您的帮助。但是,我找到了解决方案:

      password: Joi.string()
            .min(8)
            .max(25)
            .regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/, 
            'password')
      

      【讨论】:

        猜你喜欢
        • 2020-12-23
        • 2020-11-24
        • 2022-01-13
        • 2021-08-31
        • 2022-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        相关资源
        最近更新 更多