@sseworld/validator
v1.0.0
Published
String validation and sanitization
Downloads
8
Maintainers
Readme
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] [![Disclose a vulnerability][huntr-image]][huntr-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 library with npm install validator
No ES6
var validator = require("validator");
validator.isEmail("[email protected]"); //=> 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("[email protected]"); //=> 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>
Contributors
Thank you to the people who have already contributed:
Validators
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.minOccurences
: Minimum number of occurrences for the seed in the string. Defaults to 1. |
| equals(str, comparison) | check if the string matches the comparison. |
| 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', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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', 'bn', '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', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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 }
when urlSafe
is true it tests the given base64 encoded string is [url safe][Base64 URL Safe]. |
| isBefore(str [, date]) | check if the string is a date that is before the specified date. |
| 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 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', '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 rejected by Gmail. If blacklisted_chars
receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If host_blacklist
is set to an array of strings and the part of the email after the @
symbol matches one of the strings defined in it, the validation fails. If host_whitelist
is set to an array of strings and the part of the email after the @
symbol matches none of the strings defined in it, the validation fails. |
| isEmpty(str [, options]) | check if the string has a length of zero.options
is an object which defaults to { ignore_whitespace: false }
. |
| isEthereumAddress(str) | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums. |
| isFloat(str [, options]) | check if the string is a float.options
is an object which can contain the keys min
, max
, gt
, and/or lt
to validate the float is within boundaries (e.g. { min: 7.22, max: 9.55 }
) it also has locale
as an option.min
and max
are equivalent to 'greater or equal' and 'less or equal', respectively while gt
and lt
are their strict counterparts.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', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']
. Locale list is validator.isFloatLocales
. |
| isFQDN(str [, options]) | check if the string is a fully qualified domain name (e.g. domain.com).options
is an object which defaults to { require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }
. If allow_wildcard
is set to true, the validator will allow domain starting with *.
(e.g. *.example.com
or *.shop.example.com
). |
| isFreightContainerID(str) | alias for isISO6346
, check if the string is a valid ISO 6346 shipping container identification. |
| isFullWidth(str) | check if the string contains any full-width chars. |
| isHalfWidth(str) | check if the string contains any half-width chars. |
| isHash(str, algorithm) | check if the string is a hash of type algorithm.Algorithm is one of ['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']
.