README
validator.js
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 is an error.
Installation and Usage
Server-side usage
Install the library with npm install 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-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 install validator-js
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} .ignoreCase specified whether the case of the substring be same or not. |
equals(str, comparison) | check if the string matches the comparison. |
isAfter(str [, date]) | check if the string is a date that's after the specified date (defaults to 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', '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', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', '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]) | check if the string contains only letters and numbers. 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', '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', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA'] ) and defaults to en-US . Locale list is validator.isAlphanumericLocales . |
isAscii(str) | check if the string contains ASCII chars only. |
isBase32(str) | check if a string is base32 encoded. |
isBase58(str) | check if a string is base58 encoded. |
isBase64(str [, options]) | check if a 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 |
isBefore(str [, date]) | check if the string is a date that's before the specified date. |
isBIC(str) | check if a string is a BIC (Bank Identification Code) or SWIFT code. |
isBoolean(str) | check if a string is a boolean. |
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) | check if the string is a credit card. |
isCurrency(str [, options]) | check if the string is a valid currency amount.options is an object which defaults to {symbol: '
|