How to Validate Data using express-validator Module in Node.js ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Validation in node.js can be easily done by using the express-validator module. This module is popular for data validation. There are other modules available in market like hapi/joi, etc but express-validator is widely used and popular among them.Steps to install express-validator module: You can install this package by using this command. npm install express-validatorAfter installation, you can check your express-validator module version in command prompt using the command. npm version express-validatorAfter that, you can just create a simple data as shown below to send the data to the server.Filename: SampleForm.ejs html <!DOCTYPE html> <html> <head> <title>Validation using Express-Validator</title> </head> <body> <h1>Demo Form</h1> <form action="saveData" method="POST"> <pre> Enter your Email : <input type="text" name="email"> <br> Enter your Name : <input type="text" name="name"> <br> Enter your Number : <input type="number" name="mobile"> <br> Enter your Password : <input type="password" name="password"> <br> <input type="submit" value="Submit Form"> </pre> </form> </body> </html> After that, you can just create a file, for example index.js as show below:Filename: index.js javascript const { check, validationResult } = require('express-validator'); const bodyparser = require('body-parser') const express = require("express") const path = require('path') const app = express() var PORT = process.env.port || 3000 // View Engine Setup app.set("views", path.join(__dirname)) app.set("view engine", "ejs") // Body-parser middleware app.use(bodyparser.urlencoded({ extended: false })) app.use(bodyparser.json()) app.get("/", function (req, res) { res.render("SampleForm"); }) // check() is a middleware used to validate // the incoming data as per the fields app.post('/saveData', [ check('email', 'Email length should be 10 to 30 characters') .isEmail().isLength({ min: 10, max: 30 }), check('name', 'Name length should be 10 to 20 characters') .isLength({ min: 10, max: 20 }), check('mobile', 'Mobile number should contains 10 digits') .isLength({ min: 10, max: 10 }), check('password', 'Password length should be 8 to 10 characters') .isLength({ min: 8, max: 10 }) ], (req, res) => { // validationResult function checks whether // any occurs or not and return an object const errors = validationResult(req); // If some error occurs, then this // block of code will run if (!errors.isEmpty()) { res.json(errors) } // If no error occurs, then this // block of code will run else { res.send("Successfully validated") } }); app.listen(PORT, function (error) { if (error) throw error console.log("Server created Successfully on PORT ", PORT) }) Steps to run the program: The project structure will look as shown below: Make sure you have a ‘view engine’. We have used “ejs” and also install express and express-validator, body-parser using following commands: npm install ejs npm install express npm install body-parser npm install express-validatorRun index.js file using below command: node index.js Open the browser and type this URL http://localhost:8080/, the fill this sample form with correct data as shown below: Then submit the form and if no error occurs, then you will see the following output: And if you try to submit the form with incorrect data, then you will see the error message as shown below: Comment More infoAdvertise with us Next Article How to Validate Data using express-validator Module in Node.js ? gouravhammad Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Node.js Write From Home Node.js-Misc +2 More Similar Reads How to Validate Data using validator Module in Node.js ? The Validator module is popular for validation. Validation is necessary to check whether the data is correct or not, so this module is easy to use and validates data quickly and easily. Feature of validator module: It is easy to get started and easy to use.It is a widely used and popular module for 2 min read How to Validate Data using joi Module in Node.js ? Joi module is a popular module for data validation. This module validates the data based on schemas. There are various functions like optional(), required(), min(), max(), etc which make it easy to use and a user-friendly module for validating the data. Introduction to joi It's easy to get started a 3 min read How to validate if input in input field is a valid date using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only a valid date is allowed i.e. there is not allowed 4 min read How to validate if input in input field must contains a seed word using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, we often need to validate input so that it must contai 5 min read How to validate if input in input field has alphabets only using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only alphabets are allowed i.e. there not allowed any 5 min read Validating Date Inputs with express-validator in Node.js In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In some cases, we want the user to type a date that must come after some given d 5 min read How to validate if input in input field has decimal number only using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only decimal numbers are allowed i.e. there not allowe 5 min read How to Update value with put in Express/Node JS? Express JS provides various HTTP methods to interact with data. Among these methods, the PUT method is commonly used to update existing resources. PrerequisitesNode JS Express JS In this article, we are going to setup the request endpoint on the server side using Express JS and Node JS. This endpoin 2 min read How to validate if input in input field has boolean value using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only boolean value i.e. true or false are allowed. We 4 min read How to validate if input in input field has ASCII characters using express-validator ? In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only Ascii characters are allowed i.e. there is not al 4 min read Like