How to Validate Data using express-validator Module in Node.js ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share 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 G gouravhammad Follow Improve Article Tags : Node.js Node.js-Misc Similar Reads REST API Introduction REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server. 7 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Node.js Tutorial Node.js is a powerful, open-source, and cross-platform JavaScript runtime environment built on Chrome's V8 engine. It allows you to run JavaScript code outside the browser, making it ideal for building scalable server-side and networking applications.JavaScript was mainly used for frontend developme 4 min read JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an 7 min read Express.js Tutorial Express.js is a minimal and flexible Node.js web application framework that provides a list of features for building web and mobile applications easily. It simplifies the development of server-side applications by offering an easy-to-use API for routing, middleware, and HTTP utilities.Built on Node. 4 min read How to Update Node.js and NPM to the Latest Version (2025) Updating Node.js and NPM to the latest version ensures the newest features, performance improvements, and security updates. This article will guide you through the steps to update Node.js and npm to the latest version on various operating systems, including Windows, macOS, and Linux.Different Method 3 min read How to Download and Install Node.js and NPM NodeJS and NPM (Node Package Manager) are essential tools for modern web development. NodeJS is the runtime environment for JavaScript that allows you to run JavaScript outside the browser, while NPM is the package manager that helps manage libraries and code packages in your projects.To run a Node. 3 min read NodeJS Introduction NodeJS is a runtime environment for executing JavaScript outside the browser, built on the V8 JavaScript engine. It enables server-side development, supports asynchronous, event-driven programming, and efficiently handles scalable network applications. NodeJS is single-threaded, utilizing an event l 5 min read Web Server and Its Types A web server is a systemâeither software, hardware, or bothâthat stores, processes, and delivers web content to users over the Internet using the HTTP or HTTPS protocol. When a userâs browser sends a request (like visiting a website), the web server responds by delivering the appropriate resources, 7 min read Top 50+ ExpressJS Interview Questions and Answers ExpressJS is a fast, unopinionated, and minimalist web framework for NodeJS, widely used for building scalable and efficient server-side applications. It simplifies the development of APIs and web applications by providing powerful features like middleware support, routing, and template engines.In t 15+ min read Like