外观
类型检查
约 942 字大约 3 分钟
2024-01-01
介绍
提供一组用于类型判断与格式校验的函数,全部返回布尔值,覆盖数字、空值、邮箱、手机号、URL、身份证、金额、中文、包含关系以及图片/视频文件判断。
基本用法
import { isNumber, isEmpty, isEmail, isMobile, isUrl, isIdCard, isAmount, isChinese, isContains, isVideo, isImage } from '@/uni_modules/uview-unix'
console.log(isMobile('13812345678')) // true
console.log(isEmail('user@example.com')) // true
console.log(isNumber(20)) // true
console.log(isEmpty('')) // trueAPI
函数列表
| 函数 | 参数 | 返回值 | 说明 |
|---|---|---|---|
| isNumber(value) | value: any | boolean | 是否为数字类型且非 NaN |
| isEmpty(value?) | value?: any | boolean | 是否为空值 |
| isEmail(value) | value: string | boolean | 是否为合法邮箱 |
| isMobile(value) | value: string | boolean | 是否为合法大陆手机号 |
| isUrl(value) | value: string | boolean | 是否为合法 URL |
| isIdCard(value) | value: string | boolean | 是否为合法 18 位身份证格式 |
| isAmount(value) | value: string | number | boolean | 是否为合法金额格式 |
| isChinese(value) | value: string | boolean | 是否全部为中文字符 |
| isContains(value, param) | value: string, param: string | boolean | 是否包含指定子串 |
| isVideo(value) | value: string | boolean | 是否为视频文件 |
| isImage(value?) | value?: string | null | boolean | 是否为图片 |
返回值
均为 boolean,true 表示符合该函数的判断条件。
isNumber
判断值是否为数字类型且不是 NaN,字符串形式的数字返回 false。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待判断的值 | any | - |
console.log(isNumber(20)) // true
console.log(isNumber('20')) // false
console.log(isNumber(NaN)) // falseisEmpty
不传参或传入 undefined、null、仅含空白的字符串、false、0、NaN、空数组、空对象时返回 true。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待判断的值 | any | - |
console.log(isEmpty('')) // true
console.log(isEmpty(' ')) // true
console.log(isEmpty(0)) // true
console.log(isEmpty([])) // true
console.log(isEmpty({})) // true
console.log(isEmpty('hello')) // falseisEmail
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的字符串 | string | - |
console.log(isEmail('user@example.com')) // true
console.log(isEmail('invalid@')) // falseisMobile
校验 11 位中国大陆手机号,支持 13/15/18/19 开头、145-149、16 系列(除 160、163)以及 170-178 号段。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的手机号 | string | - |
console.log(isMobile('13812345678')) // true
console.log(isMobile('12345678901')) // falseisUrl
支持 http、https、ftp、rtsp、mms 协议,允许 IP 或域名、端口以及路径和查询参数。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的 URL | string | - |
console.log(isUrl('https://www.example.com/path?q=1')) // true
console.log(isUrl('ftp://192.168.1.1:21')) // true
console.log(isUrl('not a url')) // falseisIdCard
仅校验 18 位身份证的格式,不校验校验位与地区码的真实性。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的身份证号 | string | - |
console.log(isIdCard('110101199001011234')) // true
console.log(isIdCard('12345')) // falseisAmount
最多两位小数,支持千分位;整数部分不能以 0 开头,小于 1 的金额需写成 0.xx。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的金额 | string | number | - |
console.log(isAmount('1234.56')) // true
console.log(isAmount('1,234.56')) // true
console.log(isAmount('0.5')) // true
console.log(isAmount('12.345')) // false
console.log(isAmount('0')) // falseisChinese
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 待验证的字符串 | string | - |
console.log(isChinese('你好世界')) // true
console.log(isChinese('hello')) // false
console.log(isChinese('你好123')) // falseisContains
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 源字符串 | string | - |
| param | 要查找的子串 | string | - |
console.log(isContains('hello world', 'world')) // true
console.log(isContains('hello world', 'china')) // falseisVideo
按扩展名判断,支持 mp4、mpg、mpeg、dat、asf、avi、rm、rmvb、mov、wmv、flv、mkv、m3u8,不区分大小写。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 文件名或 URL | string | - |
console.log(isVideo('movie.mp4')) // true
console.log(isVideo('https://cdn.com/a.m3u8')) // true
console.log(isVideo('photo.jpg')) // falseisImage
支持图片 URL 与 base64 Data URI;普通 URL 会先去除 ? 后的查询参数再判断扩展名,支持 jpeg、jpg、gif、png、svg、webp、jfif、bmp、dpg。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 文件名、URL 或 base64 字符串 | string | null | - |
console.log(isImage('photo.jpg')) // true
console.log(isImage('https://cdn.com/a.png?size=large')) // true
console.log(isImage('data:image/png;base64,iVBORw0...')) // true
console.log(isImage('video.mp4')) // false