Form Data Validation in Node.js With Validator.js

Introduction

Almost every website involves collecting user input through html forms. Whenever we collect user data, we have to validate it. In Computer Science, data validation is the process of checking whether the data entered is sensible and reasonable.

Consider developing a form for email subscriptions. In this case, we want to check whether the submitted email follows a generic email format like [email protected]. Without validation, malicious users can enter useless values or even perform SQL injections.

In this article, we'll be performing form data validation using Validator.js - a lightweight NPM package built exactly for this purpose.

Installing Validator.js

Let's create a directory for our project, named string-validator, move into it, create an index.js entry point and initialize a Node project with the default settings using npm:

$ mkdir string-validator
$ cd string-validator
$ npm init -y

Then, let's:

$ touch index.js

After initializing the project, we can install the Validator.js package using:

$ npm install validator

Form Data Validation Using Validator.js

Consider an application for selling books where user input is first gathered using an HTML form. This data is then sent, in JSON format, to a server for further processing. Let's see how string validation can be done for this.

Let's say that this is the data that is being sent from the front-end. Let's save this data in the index.js file. Also, let's require Validator.js in our file:

const validator = require("validator")

const data = {
    "gender": "male",
    "name": {
        "title": "mr",
        "first": "brad",
        "last": "gibson"
    },
    "countryCode": "IE",
    "postalCode": "93027",
    "email": "[email protected]",
    "cell": "081-454-0666",
    "dob": "1993-07-20T09:44:18.674Z",
    "creditCardNumber": "4539415755005206",
    "book": {
        "title": "Harry Potter and the Deathly Hallows",
        "author": "Rowling, J. K.",
        "isbn": "9780545010221",
        "isbnVersion": "13"
    }
}

Validator.js has a lot of pre-set functions to check for validity of different inputs, such as credit cards, mobile phones, emails, postal codes, etc.

First, of all let's check whether the user is giving us valid credit card details. This can be done using:

console.log(validator.isCreditCard(data.creditCardNumber)); // true

In order to deliver the book, the postal code has to be valid. So, let's check that using:

console.log(validator.isPostalCode(data.postalCode, data.countryCode)) // false

Phone number validation can be done using:

console.log(validator.isMobilePhone(data.cell, `en-${data.countryCode}`)); // false

Email validation can be done using:

console.log(validator.isEmail(data.email)) // true

Also, we can do ISBN validation by using:

console.log(validator.isISBN(data.book.isbn, data.book.isbnVersion)) // true

Then, we can prompt the user to re-enter any invalid information to make sure we have clean, workable data.

Conclusion

In this article, we've gone over the Validator.js NPM package - which is a lightweight package used for data and string validation.

Author image
India Website
Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!