Password validation using Joi

Viewed 3069

I am trying to validate the password entered by the user. The password should have a minimum length of 4 and should contain both characters and numbers.I am using Joi npm module for validation.

app.js code:

const schema = Joi.object({
  email: Joi.string().email().required(),
  username: Joi.string().alphanum().min(3).required(),
  password: Joi.string().min(4).required(),
});
app.post('/register',async (req,res)=>{
  try{
       const value = await schema.validateAsync({
         email: req.body.email,
         username: req.body.username,
         password: req.body.password,
        });

  }catch(e){ 
  console.log(e)}
})

How do I check if both characters and numbers are present in the user's password? I am not using Regex expression here. Can somebody help?

2 Answers

Use confirmPassword: Joi.ref('password') instead.

Related