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

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:

  1. It is easy to get started and easy to use.
  2. It is widely used and popular module for validation.
  3. Simple functions for validation like isEmail(), isEmpty(), etc.

Installation of validator module:

  1. You can visit the link Install validator module. You can install this package by using this command.
    npm install validator
  2. After installing validator module you can check your validator version in command prompt using the command.
    npm version validator
  3. 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

Filename: index.js





const validator = require('validator')
// Check whether given email is valid or not
var email = ''
console.log(validator.isEmail(email))  // true
email = 'test@'
console.log(validator.isEmail(email))  // false
// Check whether string is in lowercase or not
var name = 'geeksforgeeks'
console.log(validator.isLowercase(name))  // true
console.log(validator.isLowercase(name))  // false
// Check whether string is empty or not
var name = ''
console.log(validator.isEmpty(name))  // true
name = '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:

  1. The project structure will look like this:
    project structure
  2. Make sure you have install express and validator module using the following commands:
    npm install validator
    npm install express
  3. Run index.js file using below command:
    node index.js

    Output of above command

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.

My Personal Notes arrow_drop_up

Recommended Posts:

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.