validator 字符串数据验证和过滤库
validator 是一个字符串验证和过滤库,能够做到我们常规应用开发中各种验证方式,比如是否包含某个字符串,字符串的长度类型等等,完全能满足我们日常的前端开发,并且还可以在服务端使用。
validator 只处理字符串和过滤工作。
如果你不确定一个字段是否是字符串,请使用
input + ''
。传递字符串以外的任何内容对象都是错误的。
安装和使用
服务器端使用
npm install validator
非 ES6
var validator = require('validator'); validator.isEmail('foo@bar.com'); //=> true
ES6
import validator from 'validator';
或者只导入库的一个子集:
import isEmail from 'validator/lib/isEmail';
客户端使用
库可以作为独立脚本加载,也可以通过 AMD
<script type="text/javascript" src="validator.min.js"></script> <script type="text/javascript"> validator.isEmail('foo@bar.com'); //=> true </script>
库也可以通过 bower 安装
$ bower install validator-js
验证器
以下是当前可用的验证器列表。
Validator | Description |
---|---|
contains(str, seed) | 验证str中是否含有seed |
equals(str, comparison) | 验证是否相等 |
isAfter(str [, date]) | 验证str是否是一个指定date之后的时间字符串,默认date为现在,与之相反的是isBefore方法 |
isAlpha(str [, locale]) | 检查字符串是否只包含字母(a-Za-Z)。 Locale is one of |
isAlphanumeric(str [, locale]) | 检查字符串是否仅包含字母和数字。 Locale is one of |
isAscii(str) | 检查字符串是否仅包含ASCII字符。 |
isBase32(str) | 检查字符串是否为base32 编码。 |
isBase64(str) | 检查字符串是否为base64 编码。 |
isBefore(str [, date]) | check if the string is a date that's before the specified date. |
isIBAN(str) | check if a string is a IBAN (International Bank Account Number). |
isBIC(str) | check if a string is a BIC (Bank Identification Code) or SWIFT code. |
isBoolean(str) | check if a string is a boolean. |
isByteLength(str [, options]) | check if the string's length (in UTF-8 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.
|
isEthereumAddress(str) | check if the string is an Ethereum address using basic regex. Does not validate address checksums. |
isBtcAddress(str) | check if the string is a valid BTC address. |
isDataURI(str) | check if the string is a data uri format. |
isDecimal(str [, options]) | 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.
|
isEmpty(str [, options]) | check if the string has a length of zero.
|
isFloat(str [, options]) | check if the string is a float.
|
isFQDN(str [, options]) | check if the string is a fully qualified domain name (e.g. domain.com).
|
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 |
isHexadecimal(str) | check if the string is a hexadecimal number. |
isHexColor(str) | check if the string is a hexadecimal color. |
isHSL(str) | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on CSS Colors Level 4 specification. Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: |
isRgbColor(str, [, includePercentValues]) | check if the string is a rgb or rgba color.
|
isIdentityCard(str [, locale]) | check if the string is a valid identity card code.
Defaults to 'any'. |
isIn(str, values) | check if the string is in a array of allowed values. |
isInt(str [, options]) | check if the string is an integer.
|
isIP(str [, version]) | check if the string is an IP (version 4 or 6). |
isIPRange(str) | check if the string is an IP Range(version 4 only). |
isISBN(str [, version]) | check if the string is an ISBN (version 10 or 13). |
isEAN(str) | check if the string is an EAN (European Article Number). |
isISIN(str) | check if the string is an ISIN (stock/security identifier). |
isISO31661Alpha2(str) | check if the string is a valid ISO 3166-1 alpha-2 officially assigned country code. |
isISO31661Alpha3(str) | check if the string is a valid ISO 3166-1 alpha-3 officially assigned country code. |
isISO8601(str) | check if the string is a valid ISO 8601 date; for additional checks for valid dates, e.g. invalidates dates like 2009-02-29 , pass options object as a second parameter with options.strict = true . |
isISSN(str [, options]) | check if the string is an ISSN.
|
isISRC(str) | check if the string is a ISRC. |
isRFC3339(str) | check if the string is a valid RFC 3339 date. |
isJSON(str) | check if the string is valid JSON (note: uses JSON.parse). |
isJWT(str) | check if the string is valid JWT token. |
isLatLong(str) | check if the string is a valid latitude-longitude coordinate in the format lat,long or lat, long . |
isLength(str [, options]) | check if the string's length falls in a range.
|
isLocale(str) | check if the string is a locale |
isLowercase(str) | check if the string is lowercase. |
isMACAddress(str) | check if the string is a MAC address.
|
isMagnetURI(str) | check if the string is a magnet uri format. |
isMD5(str) | check if the string is a MD5 hash. Please note that you can also use the |
isMimeType(str) | check if the string matches to a valid MIME type format |
isMobilePhone(str [, locale [, options]]) | check if the string is a mobile phone number, (locale is either an array of locales (e.g
|
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. |
isNumeric(str [, options]) | check if the string contains only numbers.
|
isOctal(str) | check if the string is a valid octal number. |
isPassportNumber(str, countryCode) | check if the string is a valid passport number relative to a specific country code. |
isPort(str) | check if the string is a valid port number. |
isPostalCode(str, locale) | check if the string is a postal code, (locale is one of |
isSemVer(str) | check if the string is a Semantic Versioning Specification (SemVer). |
isSurrogatePair(str) | check if the string contains any surrogate pairs chars. |
isURL(str [, options]) | check if the string is an URL.
require_protocol - if set as true isURL will return false if protocol is not present in the URL. |
isUppercase(str) | check if the string is uppercase. |
isUUID(str [, version]) | check if the string is a UUID (version 3, 4 or 5). |
isVariableWidth(str) | check if the string contains a mixture of full and half-width chars. |
isWhitelisted(str, chars) | checks characters if they appear in the whitelist. |
matches(str, pattern [, modifiers]) | check if string matches the pattern. Either |
过滤器
Here is a list of the sanitizers currently available.
Sanitizer | Description |
---|---|
blacklist(input, chars) | remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. blacklist(input, '\\[\\]') . |
escape(input) | replace < , > , & , ' , " and / with HTML entities. |
unescape(input) | replaces HTML encoded entities with < , > , & , ' , " and / . |
ltrim(input [, chars]) | trim characters from the left-side of the input. |
normalizeEmail(email [, options]) | canonicalizes an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand)
|
rtrim(input [, chars]) | trim characters from the right-side of the input. |
stripLow(input [, keep_new_lines]) | remove characters with a numerical value < 32 and 127, mostly control characters. If keep_new_lines is true , newline characters are preserved (\n and \r , hex 0xA and 0xD ). Unicode-safe in JavaScript. |
toBoolean(input [, strict]) | convert the input string to a boolean. Everything except for '0' , 'false' and '' returns true . In strict mode only '1' and 'true' return true . |
toDate(input) | convert the input string to a date, or null if the input is not a date. |
toFloat(input) | convert the input string to a float, or NaN if the input is not a float. |
toInt(input [, radix]) | convert the input string to an integer, or NaN if the input is not an integer. |
trim(input [, chars]) | trim characters (whitespace by default) from both sides of the input. |
whitelist(input, chars) | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. whitelist(input, '\\[\\]') . |
isSlug | Check if the string is of type slug. Options allow a single hyphen between string. e.g. [cn-cn , cn-c-c ] |
相关链接
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论