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.
Filename: repository.js This file contains all the logic to create a local database and interact with it.
Filename: signup.js This file contains logic to show sign up form.
Filename: validator.js This file contain all the validation logic (Logic to see if password and passwordConfirm are same).
Database after successful Sign Up(Sign Up with same password and confirm password inputs)
Note: We have used some Bulma classes(CSS framework) in the signup.js file to design the content.
- How to get the same behavior with confirm and bootstrap modal?
- React.js | Uncontrolled Vs Controlled Inputs
- How to Sign In with raw password when password store in the database are hashed form in Node.js ?
- Node.js | Buffer.compare() Method
- Password Strength Indicator using jQuery
- MySQL | Change User Password
- HTML | DOM Input Password Object
- jQuery | :password Selector
- HTML | DOM Input Password type Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.