Validator.js
String validation
Install / Use
/learn @validatorjs/Validator.jsREADME
validator.js
[![NPM version][npm-image]][npm-url]
[![CI][ci-image]][ci-url]
[![Coverage][codecov-image]][codecov-url]
[![Downloads][downloads-image]][npm-url]
[![Gitter][gitter-image]][gitter-url]
A library of string validators and sanitizers.
Strings only
This library validates and sanitizes strings only.
If you're not sure if your input is a string, coerce it using input + ''.
Passing anything other than a string will result in an error.
Installation and Usage
Server-side usage
Install the validator package as:
npm i validator
yarn add validator
pnpm i validator
No ES6
var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true
ES6
import validator from 'validator';
Or, import only a subset of the library:
import isEmail from 'validator/lib/isEmail';
Tree-shakeable ES imports
import isEmail from 'validator/es/lib/isEmail';
Client-side usage
The library can be loaded either as a standalone script, or through an [AMD][amd]-compatible loader
<script type="text/javascript" src="validator.min.js"></script>
<script type="text/javascript">
validator.isEmail('foo@bar.com'); //=> true
</script>
The library can also be installed through [bower][bower]
$ bower install validator-js
CDN
<script src="https://unpkg.com/validator@latest/validator.min.js"></script>
Validators
Here is a list of the validators currently available.
Validator | Description
--------------------------------------- | --------------------------------------
contains(str, seed [, options]) | check if the string contains the seed.<br/><br/>options is an object that defaults to { ignoreCase: false, minOccurrences: 1 }.<br />Options: <br/> ignoreCase: Ignore case when doing comparison, default false.<br/>minOccurrences: Minimum number of occurrences for the seed in the string. Defaults to 1.
equals(str, comparison) | check if the string matches the comparison.
isAbaRouting(str) | check if the string is an ABA routing number for US bank account / cheque.
isAfter(str [, options]) | check if the string is a date that is after the specified date.<br/><br/>options is an object that defaults to { comparisonDate: Date().toString() }.<br/>Options:<br/>comparisonDate: Date to compare to. Defaults to Date().toString() (now).
isAlpha(str [, locale, options]) | check if the string contains only letters (a-zA-Z).<br/><br/>locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'th-TH', 'tr-TR', 'uk-UA'] and defaults to en-US. Locale list is validator.isAlphaLocales. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
isAlphanumeric(str [, locale, options]) | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'bn-IN', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'gu-IN', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'kn-IN', 'ko-KR', 'ku-IQ', 'ml-IN', 'nb-NO', 'nl-NL', 'nn-NO', 'or-IN', 'pa-IN', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'ta-IN', 'te-IN', 'th-TH', 'tr-TR', 'uk-UA']) and defaults to en-US. Locale list is validator.isAlphanumericLocales. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
isAscii(str) | check if the string contains ASCII chars only.
isBase32(str [, options]) | check if the string is base32 encoded. options is optional and defaults to { crockford: false }.<br/> When crockford is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32].
isBase58(str) | check if the string is base58 encoded.
isBase64(str [, options]) | check if the string is base64 encoded. options is optional and defaults to { urlSafe: false, padding: true }<br/> when urlSafe is true default value for padding is false and it tests the given base64 encoded string is [url safe][Base64 URL Safe].
isBefore(str [, options]) | check if the string is a date that is before the specified date.<br/><br/>options is an object that defaults to { comparisonDate: Date().toString() }.<br/><br/>Options:<br/>comparisonDate: Date to compare to. Defaults to Date().toString() (now).
isBIC(str) | check if the string is a BIC (Bank Identification Code) or SWIFT code.
isBoolean(str [, options]) | check if the string is a boolean.<br/>options is an object which defaults to { loose: false }. If loose is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (e.g.: ['true', 'True', 'TRUE']).
isBtcAddress(str) | check if the string is a valid BTC address.
isByteLength(str [, options]) | check if the string's length (in UTF-8 bytes) falls in a range.<br/><br/>options is an object which defaults to { min: 0, max: undefined }.
isCreditCard(str [, options]) | check if the string is a credit card number.<br/><br/> options is an optional object that can be supplied with the following key(s): provider is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include ['amex', 'dinersclub', 'discover', 'jcb', 'mastercard', 'unionpay', 'visa'] or blank will check for any provider.
isCurrency(str [, options]) | check if the string is a valid currency amount.<br/><br/>options is an object which defaults to { symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false }.<br/>Note: The array digits_after_decimal is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
isDataURI(str) | check if the string is a [data uri format][Data URI Format].
isDate(str [, options]) | check if the string is a valid date. e.g. [2002-07-15, new Date()].<br/><br/> options is an object which can contain the keys format, strictMode and/or delimiters.<br/><br/>format is a string and defaults to YYYY/MM/DD.<br/><br/>strictMode is a boolean and defaults to false. If strictMode is set to true, the validator will reject strings different from format.<br/><br/> delimiters is an array of allowed date delimiters and defaults to ['/', '-'].
isDecimal(str [, options]) | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.<br/><br/>options is an object which defaults to {force_decimal: false, decimal_digits: '1,', locale: 'en-US'}.<br/><br/>locale determines the decimal separator and is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN'].<br/>Note: decimal_digits is given as a range like '1,3', a specific value like '3' or min like '1,'.
isDivisibleBy(str, number) | check if the string is a number that is divisible by another.
isEAN(str) | check if the string is an [EAN (European Article Number)][European Article Number].
isEmail(str [, options]) | check if the string is an email.<br/><br/>options is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, allow_underscores: false, domain_specific_validation: false, blacklisted
