validate 格式校验
常见格式的正则校验:邮箱、手机号、身份证、IP、颜色、图片、cron 表达式等。所有函数接收字符串(或对应类型),返回布尔值,不抛异常。
函数一览
| 函数 | 说明 | 示例 |
|---|---|---|
isEmail(str) | 邮箱 | isEmail('a@b.com') // true |
isPhoneNumber(str) | 手机号(中国大陆) | isPhoneNumber('13812345678') // true |
isChinese(str) | 全汉字 | isChinese('中国') // true |
isIdCard(str) | 身份证(15/18 位) | isIdCard('110101199003077534') // true |
isQQ(str) | QQ 号 | isQQ('1422000000') // true |
isPostalCode(str) | 中国邮政编码 | isPostalCode('100000') // true |
isIPv4(str) | IPv4 地址 | isIPv4('192.168.0.1') // true |
isIPv6(str) | IPv6 地址 | isIPv6('2001:db8::1') // true |
isHexNumber(str) | 16 进制数字(0x 开头) | isHexNumber('0xFF') // true |
isJson(str) | 合法 JSON 对象 | isJson('{}') // true |
isCron(str) | cron 表达式(6 字段含秒) | isCron('0 0 * * * *') // true |
isSvg(val) | SVG 片段 | isSvg('<svg></svg>') // true |
isImgPath(path) | 图片链接 | isImgPath('https://a.com/i.png') // true |
isBase64Img(str) | Base64 图片 | isBase64Img('data:image/png;base64,...') // true |
isLessThanIntMax(val) | 小于 int 最大值(2147483647) | isLessThanIntMax(2147483647) // true |
isHexColor(color) | 十六进制色值 | isHexColor('#fff') // true |
isRgbColor(color) | RGB 色值 | isRgbColor('rgb(255,0,0)') // true |
isRgbaColor(color) | RGBA 色值 | isRgbaColor('rgba(255,0,0,0.5)') // true |
isHslColor(color) | HSL 色值 | isHslColor('hsl(0,100%,50%)') // true |
isHslaColor(color) | HSLA 色值 | isHslaColor('hsla(0,100%,50%,0.5)') // true |
isCssNameColor(color) | CSS 命名色 | isCssNameColor('red') // true |
isColor(color) | 任意合法 CSS 色值 | isColor('#fff') // true |
类型签名
ts
function isImgPath(path: string): boolean
function isLessThanIntMax(val: number | string): boolean
function isHexColor(color: string): boolean
function isRgbColor(color: string): boolean
function isRgbaColor(color: string): boolean
function isHslColor(color: string): boolean
function isHslaColor(color: string): boolean
function isCssNameColor(color: string): boolean
function isColor(color: string): boolean
function isBase64Img(str: string): boolean
function isEmail(str: string): boolean
function isPhoneNumber(str: string): boolean
function isChinese(str: string): boolean
function isIdCard(str: string): boolean
function isQQ(str: string): boolean
function isPostalCode(str: string): boolean
function isIPv4(str: string): boolean
function isIPv6(str: string): boolean
function isHexNumber(str: string): boolean
function isJson(str: string): boolean
function isCron(str: string): boolean
function isSvg(val: string): boolean示例
格式校验演示
输入任意内容,实时校验多种格式:
isEmail('13812345678')falseisPhoneNumber('13812345678')trueisIPv4('13812345678')falseisColor('13812345678')falseisIdCard('13812345678')false