The registration or Sign Up in any website always requires a confirm password input and it must be the same as the password. It is basically to ensure that the user enters the password full of his sense and there is no conflict happens. This functionality can be implemented anywhere in our code like in index file or route file but this comes under the validation part. So we usually prefer to code this logic where all the other validations are coded. Here we use ‘express-validator’ middleware to implement this functionality.
Command to install express-validator
npm install express-validator
Steps to use express-validator to implement the logic:
Install express-validator middleware.
Create a validator.js file to code all the validation logic.
Validate confirmPassword by validateConfirmPassword: check(‘confirmPassword’) and chain on all the validation with ‘ . ‘
Use the validation name(validateConfirmPassword) in the routes as a middleware as an array of validations.
Destructure ‘validationResult’ function from express-validator to use it to find any errors
If error occurs redirect to the same page passing the error information
If error list is empty, give access to the user for the subsequent request.
Note: Here we use local or custom database to implement the logic, the same steps can be followed to implement the logic in a regular database like MongoDB or MySql.
Example 1: This example illustrates how to check if email address is already in use or not for a particular website.
Please Login to comment...