README
validator.js
A library of string validators and sanitizers.
No pain
Everything will be the string
Server-side usage
Install the library with npm install validator
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';
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
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.
Validators
- contains(str, seed) - check if the string contains the seed.
- 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]) - 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', 'cs-CZ', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']
) and defaults toen-US
. - 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', 'cs-CZ', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'tr-TR']
) and defaults toen-US
. - isAscii(str) - check if the string contains ASCII chars only.
- isBase64(str) - check if a string is base64 encoded.
- isBefore(str [, date]) - check if the string is a date that's before the specified date.
- isBoolean(str) - check if a string is a boolean.
- isByteLength(str, options) - check if the string's length (in 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: '