Skip to content

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')false
isPhoneNumber('13812345678')true
isIPv4('13812345678')false
isColor('13812345678')false
isIdCard('13812345678')false