Skip to content

Guide · Open source

How to use validator.js: install and validate strings

Updated · written with AI from the project's own documentation and checked against our catalogue data

validator.js is a JavaScript library of string validators and sanitizers. It checks whether a string is a format such as an email address, a date or a credit card number. This guide shows you how to install it, import the functions you need and use them in Node.js or the browser.

validator.jsvalidatorjs/validator.js · ★ 23.7k · MIT

Quick start

  1. 1Install the package. Run npm i validator in your project folder.
  2. 2Confirm your input is a string. Coerce values with input + '' before validating, since non-string input causes an error.
  3. 3Import the validator you need. Import the whole library or a single function such as isEmail from validator/lib/isEmail.
  4. 4Call the validator. Pass the string to a function like isEmail or isDate, which returns true or false.
  5. 5Add options if needed. Pass an options object for validators such as isDate or isCreditCard to match your format rules.

Install validator.js with npm

validator.js is installed as the validator package through npm. The README also lists yarn and pnpm as options for the same package.

The library runs on Node.js or in a browser environment.

npm i validator

Use validator.js in Node.js with require or import

In older CommonJS code, load the whole library with require. Each validator is then a function on the returned object. The README example calls isEmail and gets true for a valid address.

In ES6 code you can import the whole library, or import a single validator to keep the bundle smaller. The README also documents a tree-shakeable import path under validator/es.

  • Whole library: import the default export from 'validator'.
  • Single validator: import isEmail from 'validator/lib/isEmail'.
  • Tree-shakeable: import isEmail from 'validator/es/lib/isEmail'.
var validator = require('validator'); validator.isEmail('foo@bar.com'); //=> true

Convert input to a string before validating

The README states that validator.js validates and sanitizes strings only. Passing anything other than a string results in an error. If you are not sure what your input is, coerce it to a string first, as the README suggests.

This matters for form or API input, where numbers, objects or empty values can arrive. Convert the value before you call any validator.

validator.isEmail(input + '');

Use validator.js in the browser

For browser projects, the library can be loaded as a standalone script. After loading it, the validator functions are available on the global validator object. The README also mentions AMD-compatible loaders, bower, and a CDN file on unpkg.

Use the CDN option only if you want to avoid a build step. Otherwise, the npm package fits a bundler-based project.

<script src="https://unpkg.com/validator@latest/validator.min.js"></script> <script> validator.isEmail('foo@bar.com'); //=> true </script>

Choose options for common validators

Many validators accept an optional second argument with settings. The README documents these for several functions. For example, isDate takes a format (defaulting to YYYY/MM/DD), a strictMode flag and a delimiters array (defaulting to / and -).

isCreditCard accepts a provider option, such as visa or mastercard, or no provider to check any issuer. isAlpha and isAlphanumeric accept a locale, which defaults to en-US, and an ignore option for characters to skip.

  • isDate: format, strictMode, delimiters.
  • isCreditCard: provider (amex, dinersclub, discover, jcb, mastercard, unionpay, visa).
  • isAlpha and isAlphanumeric: locale and ignore.
  • contains: ignoreCase and minOccurrences.

What people use it for

  • Checking email addresses on a sign-up form in a Node.js server.
  • Validating a date field in an API request before saving it.
  • Checking that a credit card number matches a provider such as Visa.
  • Validating that a user name contains only letters with isAlpha and a chosen locale.

Frequently asked questions

How do I install validator.js?

Install it with npm by running npm i validator. The README also lists yarn add validator and pnpm i validator as alternatives for the same package.

Does validator.js work in the browser?

Yes. The README says it can be loaded as a standalone script or through an AMD-compatible loader, and it provides a unpkg CDN file. It can also be installed through bower.

Can validator.js check numbers or objects?

No. The README says the library validates and sanitizes strings only, and passing anything other than a string results in an error. Convert the value to a string first with input + ''.

Is validator.js free to use?

The project is licensed under the MIT licence, as shown in the README badge and the project's LICENSE file.

How do I check an email address with validator.js?

Import the library and call validator.isEmail with the string. It returns true for a valid address and false otherwise. You can also import only isEmail from validator/lib/isEmail.

What are alternatives to validator.js?

Comparable projects named in our analysis include joi, zod, express-validator and yup. validator.js focuses on individual string checks and sanitizers rather than schema definitions.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

validator.js suits JavaScript developers who need ready-made string checks such as isEmail or isDate on Node.js or in the browser, without writing their own regular expressions. Pick something else if you need to validate numbers or objects directly, work outside JavaScript, or want a no-code interface.

Install options & downloads