reactjsvalidationschemajoi

How to do Password validation in react js using joi schema?


I want to validate the password rules as: Password should be 8 characters long, should have 1 upper case and 1 lower case, 1 number and 1 special character.

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;
  }),

I tried these but passwordComplexity I guess works only with node js. Any help on this?


Solution

  • Thank You for the help. However, I found a solution to this as:

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