How to Validate Data using validator Module in Node.js ? - GeeksforGeeks

27 Apr, 2020
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 widely used and popular module for validation.Simple functions for validation like isEmail(), isEmpty(), etc.
It is easy to get started and easy to use.
It is widely used and popular module for validation.
Simple functions for validation like isEmail(), isEmpty(), etc.
Installation of validator module:
You can visit the link Install validator module. You can install this package by using this command.npm install validatorAfter installing validator module you can check your validator version in command prompt using the command.npm version validatorAfter that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.node index.js
You can visit the link Install validator module. You can install this package by using this command.npm install validator
npm install validator
After installing validator module you can check your validator version in command prompt using the command.npm version validator
npm version validator
After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.node index.js
node index.js
Filename: index.js
const validator = require('validator')  // Check whether given email is valid or notvar email = 'test@gmail.com'console.log(validator.isEmail(email))  // trueemail = 'test@'console.log(validator.isEmail(email))  // false  // Check whether string is in lowercase or notvar name = 'geeksforgeeks'console.log(validator.isLowercase(name))  // truename = 'GEEKSFORGEEKS'console.log(validator.isLowercase(name))  // false  // Check whether string is empty or notvar name = ''console.log(validator.isEmpty(name))  // truename = 'geeksforgeeks'console.log(validator.isEmpty(name))  // false  // Other functions also available in // this module like isBoolean()// isCurrency(), isDecimal(), isJSON(),// isJWT(), isFloat(), isCreditCard(), etc.
Steps to run the program:
The project structure will look like this:Make sure you have install express and validator module using the following commands:npm install validator
npm install expressRun index.js file using below command:node index.js
The project structure will look like this:
Make sure you have install express and validator module using the following commands:npm install validator
npm install express
npm install validator
npm install express
Run index.js file using below command:node index.js
node index.js
So this is how you can use the validator module for validation. There are also other modules available in market for validation like hapi-joi, express-validator, etc.
Node.js-Misc
Node.js
Web Technologies
Web technologies Questions
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Difference between npm i and npm ci in Node.js
Mongoose Populate() Method
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript