The registration or Sign Up in any website always requires a username. Most of the time we use ’email’ to register on a website. The registration email is always unique and must refer to only one user otherwise conflict between the users can happen. To solve this conflict every website must have the functionality to not accept the email that already exists on the website. 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 email by validateEmail : check(’email’) and chain on all the validation with ‘ . ‘
- Use the validation name(validateEmail) 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: 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 email already exist or not).
Run index.js file using the following command:
Database after successful Sign Up(Sign Up with email that not in use)
Note: We have used some Bulma classes (CSS framework) in the signup.js file to design the content.
- Nodejs | Automatic restart NodeJs server with nodemon
- How to validate an Email using PHP?
- How to send an email using PHPMailer ?
- Email Template using HTML and CSS
- How to send email with Nodemailer using Gmail account in Node.js ?
- How to Send Email using Mailgun API in Node.js ?
- How to send Attachments and Email using nodemailer in Node.js ?
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Signup Form Using Nodejs and MongoDB
- Nodejs | authentication using Passportjs and passport-local-mongoose
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
- Generating Lighthouse Scores using NodeJS
- How to Get Local IP Address of System using PHP ?
- PHP | Send Attachment With Email
- Types of Email Attacks
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.