Using express-validator to validate password to have minimum 8 characters including at least one special character and at least one number

Q How to use express-validator to validate passwords to have minimum 8 characters including at least one special character at least one number? A The regular expression used to validate a password which will follow the above-mentioned pattern is as follows

While using express-validator, we use  the chain method .matches() to check the input password against regular expression as follows

The above validator checks whether the request body of the post request has a password which is a minimum of 8 characters long, and contains an alphabet and a special character.   If there are any validation errors, it can be obtained using  validationResult(req)  

To display only the first error at a time we add the following object to errors.array() 

Now handling the error part looks like this

So the final code looks like this

 

Share this page in social media platforms