[![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.
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.
Install the validator package as:
npm i validator
yarn add validator
pnpm i validator
var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true
import validator from 'validator';
Or, import only a subset of the library:
import isEmail from 'validator/lib/isEmail';
import isEmail from 'validator/es/lib/isEmail';
The library can be loaded either as a standalone script, or through an [AMD][amd]-compatible loader
<script type="text/javascript" src="https://raw.githubusercontent.com/validatorjs/validator.js/HEAD/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>
Here is a list of the validators currently available.
| Validator | Description |
|---|---|
| contains(str, seed [, options]) | check if the string contains the seed.options is an object that defaults to { ignoreCase: false, minOccurrences: 1 }.Options: ignoreCase: Ignore case when doing comparison, default false.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.options is an object that defaults to { comparisonDate: Date().toString() }.Options: comparisonDate: Date to compare to. Defaults to Date().toString() (now). |
| isAlpha(str [, locale, options]) | check if the string contains only letters (a-zA-Z).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).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 }.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 }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.options is an object that defaults to { comparisonDate: Date().toString() }.Options: 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.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.options is an object which defaults to { min: 0, max: undefined }. |
| isCreditCard(str [, options]) | check if the string is a credit card number.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.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 }.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()].options is an object which can contain the keys format, strictMode and/or delimiters.format is a string and defaults to YYYY/MM/DD.strictMode is a boolean and defaults to false. If strictMode is set to true, the validator will reject strings different from format.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.options is an object which defaults to {force_decimal: false, decimal_digits: '1,', locale: 'en-US'}.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'].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.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_chars: '', host_blacklist: [] }. If allow_display_name is set to true, the validator will also match Display Name <email-address>. If require_display_name is set to true, the validator will reject strings without the format Display Name <email-address>. If allow_utf8_local_part is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If require_tld is set to false, email addresses without a TLD in their domain will also be matched. If ignore_max_length is set to true, the validator will not check for the standard max length of an email. If allow_ip_domain is set to true, the validator will allow IP addresses in the host part. If domain_specific_validation is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are r |
| 日期 | 榜单 | 排名 | 周期新增 | 总 STAR |
|---|