Node浏览器JavaScript中字符串校验工具库(Node浏览器JavaScript中字符串校验工具库)

介绍

validator.js是一个Github上专门针对字符串校验所建立的一个工具库,validator.js包含了大量实用的字符串校验和清洗方法,并且支持在Node环境和浏览器环境使用,非常实用。

Node浏览器JavaScript中字符串校验工具库(Node浏览器JavaScript中字符串校验工具库)(1)

Github

https://Github.com/validatorjs/validator.js

安装实用

包管理方式安装

npm install validator //或者 yarn add validator

可以在各种开发场景下使用

  • 不在es6环境下的模块化

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

  • es6的模块化

可以一次性全部导入

import validator from 'validator';

也可以针对不同方法单独引入,比如我们只需要导入验证邮箱的方法

import isEmail from 'validator/lib/isEmail';

或者Tree-shakeable ES imports导入

import isEmail from 'validator/es/lib/isEmail';

  • 浏览器直接使用

<script type="text/javascript" src="validator.min.js"></script> <script type="text/javascript"> validator.isEmail('foo@bar.com'); //=> true </script>

方法列表

方法非常丰富,可以根据需要进行使用,建议先看一下Github上中每个方法的含义和使用方式。

import toDate from './lib/toDate'; import toFloat from './lib/toFloat'; import toInt from './lib/toInt'; import toBoolean from './lib/toBoolean'; import equals from './lib/equals'; import contains from './lib/contains'; import matches from './lib/matches'; import isEmail from './lib/isEmail'; import isURL from './lib/isURL'; import isMACAddress from './lib/isMACAddress'; import isIP from './lib/isIP'; import isIPRange from './lib/isIPRange'; import isFQDN from './lib/isFQDN'; import isDate from './lib/isDate'; import isBoolean from './lib/isBoolean'; import isLocale from './lib/isLocale'; import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha'; import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric'; import isNumeric from './lib/isNumeric'; import isPassportNumber from './lib/isPassportNumber'; import isPort from './lib/isPort'; import isLowercase from './lib/isLowercase'; import isUppercase from './lib/isUppercase'; import isIMEI from './lib/isIMEI'; import isAscii from './lib/isAscii'; import isFullWidth from './lib/isFullWidth'; import isHalfWidth from './lib/isHalfWidth'; import isVariableWidth from './lib/isVariableWidth'; import isMultibyte from './lib/isMultibyte'; import isSemVer from './lib/isSemVer'; import isSurrogatePair from './lib/isSurrogatePair'; import isInt from './lib/isInt'; import isFloat, { locales as isFloatLocales } from './lib/isFloat'; import isDecimal from './lib/isDecimal'; import isHexadecimal from './lib/isHexadecimal'; import isOctal from './lib/isOctal'; import isDivisibleBy from './lib/isDivisibleBy'; import isHexColor from './lib/isHexColor'; import isRgbColor from './lib/isRgbColor'; import isHSL from './lib/isHSL'; import isISRC from './lib/isISRC'; import isIBAN, { locales as ibanLocales } from './lib/isIBAN'; import isBIC from './lib/isBIC'; import isMD5 from './lib/isMD5'; import isHash from './lib/isHash'; import isJWT from './lib/isJWT'; import isJSON from './lib/isJSON'; import isEmpty from './lib/isEmpty'; import isLength from './lib/isLength'; import isByteLength from './lib/isByteLength'; import isUUID from './lib/isUUID'; import isMongoId from './lib/isMongoId'; import isAfter from './lib/isAfter'; import isBefore from './lib/isBefore'; import isIn from './lib/isIn'; import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; import isEAN from './lib/isEAN'; import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; import isEthereumAddress from './lib/isEthereumAddress'; import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; import isISO31661Alpha2 from './lib/isISO31661Alpha2'; import isISO31661Alpha3 from './lib/isISO31661Alpha3'; import isISO4217 from './lib/isISO4217'; import isBase32 from './lib/isBase32'; import isBase58 from './lib/isBase58'; import isBase64 from './lib/isBase64'; import isDataURI from './lib/isDataURI'; import isMagnetURI from './lib/isMagnetURI'; import isMimeType from './lib/isMimeType'; import isLatLong from './lib/isLatLong'; import isPostalCode, { locales as isPostalCodeLocales } from './lib/isPostalCode'; import ltrim from './lib/ltrim'; import rtrim from './lib/rtrim'; import trim from './lib/trim'; import escape from './lib/escape'; import unescape from './lib/unescape'; import stripLow from './lib/stripLow'; import whitelist from './lib/whitelist'; import blacklist from './lib/blacklist'; import isWhitelisted from './lib/isWhitelisted'; import normalizeEmail from './lib/normalizeEmail'; import isSlug from './lib/isSlug'; import isLicensePlate from './lib/isLicensePlate'; import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT';

总结

validator.js是一个非常值得学习的工具库,不仅是使用它,也可以用它来学习一些JavaScript的基础知识,学习其中各类方法的写法,也可以巩固JavaScript的基础知识!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页