A library of string validators and sanitizers.

Server-side usage

Install the library with (npm install validator)

1
2
3
var validator = require('validator');
 
validator.isEmail('foo@bar.com'); //=> true

Client-side usage

The library can be loaded either as a standalone script, or through an AMD-compatible loader

1
2
3
4
<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

1
$ bower install validator-js

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) - check if the string contains only letters (a-zA-Z).
  • isAlphanumeric(str) - check if the string contains only letters and numbers.
  • 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, min [, max]) - check if the string's length (in bytes) falls in a range.
  • 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
  • isDate(str) - check if the string is a date.
  • isDecimal(str) - check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.
  • isDivisibleBy(str, number) - check if the string is a number that's divisible by another.
  • isEmail(str [, options]) - check if the string is an email. options is an object
  • isFQDN(str [, options]) - check if the string is a fully qualified domain name (e.g. domain.com).
  • isFloat(str [, options]) - check if the string is a float.
  • isFullWidth(str) - check if the string contains any full-width chars.
  • isHalfWidth(str) - check if the string contains any half-width chars.
  • isHexColor(str) - check if the string is a hexadecimal color.
  • isHexadecimal(str) - check if the string is a hexadecimal number.
  • isIP(str [, version]) - check if the string is an IP (version 4 or 6).
  • isISBN(str [, version]) - check if the string is an ISBN (version 10 or 13).
  • isISIN(str) - check if the string is an ISIN (stock/security identifier).
  • isISO8601(str) - check if the string is a valid ISO 8601 date.
  • isIn(str, values) - check if the string is in a array of allowed values.
  • isInt(str [, options]) - check if the string is an integer. options is an object which can contain
  • isJSON(str) - check if the string is valid JSON (note: uses JSON.parse).
  • isLength(str, min [, max]) - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
  • isLowercase(str) - check if the string is lowercase.
  • isMobilePhone(str, locale) - check if the string is a mobile phone number
  • isMongoId(str) - check if the string is a valid hex-encoded representation of a MongoDB ObjectId.
  • isMultibyte(str) - check if the string contains one or more multibyte chars.
  • isNull(str) - check if the string is null.
  • isNumeric(str) - check if the string contains only numbers.
  • isSurrogatePair(str) - check if the string contains any surrogate pairs chars.
  • isURL(str [, options]) - check if the string is an URL. options is an object
  • isUUID(str [, version]) - check if the string is a UUID (version 3, 4 or 5).
  • isUppercase(str) - check if the string is uppercase.
  • isVariableWidth(str) - check if the string contains a mixture of full and half-width chars.
  • matches(str, pattern [, modifiers]) - check if string matches the pattern.

更多详情参考 https://github.com/chriso/validator.js