Validator

Description:
  • 数据验证

Source:
Since:
  • 1.1.0

数据验证

Methods

(static) isBankCard(value, optionsopt) → {boolean}

Description:
  • 检测值是否为银行卡号。正常模式(非0开头,10-21位数字)宽松模式(8-30位数字)

Source:
Since:
  • 1.1.0
See:
Example
isBankCard('6228480402564890018'); // true
isBankCard('6228480402564890'); // true
isBankCard('123456789'); // false

// 宽松模式
isBankCard('123456789', { loose: true }); // true
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
loose boolean <optional>
false

宽松模式,8-30位数字

luhn boolean <optional>
false

使用 Luhn 算法校验校验码

Returns:

值是否为银行卡号

Type
boolean

(static) isBusinessLicense(value, optionsopt) → {boolean}

Description:
  • 检测值是否为营业执照号,也叫工商注册号。由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。

Source:
Since:
  • 3.5.0
See:
Example
isBusinessLicense('310115600985533'); // true
isBusinessLicense('310115600985535'); // false

// 不校验验证码,长度和类型还是有校验
isBusinessLicense('310115600985535', { checkCode: false }); // true
isBusinessLicense('ac115600985535', { checkCode: false }); // false
isBusinessLicense('31011560098', { checkCode: false }); // false
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
checkCode boolean <optional>
true

是否校验最后一位校验码,如果为false,不校验校验位。

Returns:

值是否为营业执照号

Type
boolean

(static) isChinese(value, optionsopt) → {boolean}

Description:
  • 检测值是否为中文

Source:
Since:
  • 1.1.0
See:
Example
isChinese('林某某'); // true
isChinese('林A'); // false

// 宽松模式,只要包含中文即为true
isChinese('林A', { loose: true }); // true
isChinese('A林A', { loose: true }); // true

// 扩展字符集的字符
isChinese('𠮷'); // false

// 使用中文扩展字符集,需要浏览器支持 RegExp.prototype.unicode 才生效。
isChinese('𠮷', { useExtend: true }); // true
isChinese('𠮷aa', { useExtend: true, loose: true }); // true
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
loose boolean <optional>
false

宽松模式。如果为true,只要包含中文即为true

useExtend boolean <optional>
false

使用统一表意文字扩展A-I。注意:如果不支持 RegExp.prototype.unicode,扩展字符集将自动不生效,如IE浏览器。

Returns:

值是否为中文

Type
boolean

(static) isEmail(value) → {boolean}

Description:
  • 检测值是否为Email

Source:
Since:
  • 1.1.0
Example
isEmail('1232@qq.com'); // true
isEmail('123@'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为Email

Type
boolean

(static) isHMCard(value) → {boolean}

Description:
  • 检测值是否为港澳居民来往内地通行证,俗称回乡证或回乡卡。

Source:
Since:
  • 4.0.0
See:
Example
// 第一代 11 位
isHMCard('h3203117707'); // true
isHMCard('H3203117707'); // true
isHMCard('m3203117707'); // true
isHMCard('M3203117707'); // true

// 第二代 9 位
isHMCard('h32031177'); // true
isHMCard('H32031177'); // true
isHMCard('m32031177'); // true
isHMCard('M32031177'); // true
Parameters:
Name Type Description
value *

要检测的值

Returns:

是否为港澳居民来往内地通行证

Type
boolean

(static) isIPv4(value) → {boolean}

Description:
  • 检测值是否为ipv4

Source:
Since:
  • 1.1.0
Example
isIPv4('192.168.1.1'); // true
isIPv4('255.255.255.255'); // true
isIPv4('256.256.256.256'); // false
isIPv4('0.0'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为ipv4

Type
boolean

(static) isIPv6(value) → {boolean}

Description:
  • 检测值是否为ipv6

Source:
Since:
  • 1.1.0
Example
// 冒分十六进制表示法
isIPv6('2001:0DB8:0000:0023:0008:0800:200C:417A'); // true

// 前导0省略
isIPv6('2001:DB8:0:23:8:800:200C:417A'); // true
isIPv6('FF01:0:0:0:0:0:0:1101'); // true

// 0位压缩表示法
isIPv6('FF01::1101'); // true
isIPv6('::1'); // true
isIPv6('::'); // true
isIPv6('0:0:0:0:0:0:0:1'); // true
isIPv6('0:0:0:0:0:0:0:0'); // true

// 内嵌IPv4地址表示法
isIPv6('::192.168.1.1'); // true
isIPv6('::FFFF:192.168.1.1'); // true
isIPv6('192.168.1.1'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为ipv6

Type
boolean

(static) isIdCard(value, optionsopt) → {boolean}

Description:
  • 检测值是否为18位身份证号码。

    宽松模式下,支持15位身份证号码。

Source:
Since:
  • 1.1.0
See:
Example
isIdCard('130701199310302288'); // true
isIdCard('13070119931030228X'); // false

// 不校验校验码
isIdCard('13070119931030228X', { checkCode: false }); // true

// 默认不支持15位身份证号码
isIdCard('320311770706001'); // false

// 宽松模式,支持15位身份证号
isIdCard('320311770706001', { loose: true }); // true
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
loose boolean <optional>
false

宽松模式,支持15位身份证号码

checkCode boolean <optional>
true

是否校验最后一位校验码,仅支持18位身份证号码

Returns:

值是否为身份证号

Type
boolean

(static) isMobile(value) → {boolean}

Description:
  • 检测值是否为手机号码

Source:
Since:
  • 1.1.0
Example
isMobile('13000000000'); // true
isMobile('13000'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为手机号码

Type
boolean

(static) isPassport(value) → {boolean}

Description:
  • 检测值是否为护照号 支持普通护照(E*)、外交护照(DE)、公务护照(SE)、公务普通护照(PE)、香港特区护照(K/KJ/H*)、澳门特区护照(MA/MB/M*),注意不区分大小写

Source:
Since:
  • 1.1.0
See:
Example
isPassport('E12345678'); // true
isPassport('abc'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为护照号

Type
boolean

(static) isPassword(value, optionsopt) → {boolean}

Description:
  • 检测值是否符合密码强度

    注意:该校验只校验是否存在不同字符(大小写字母、数字、特殊符号),不判断长度。

    如果需要更细致的验证,请使用 validatePassword

Source:
Since:
  • 1.1.0
See:
Example
isPassword('a12345678'); // true

// 3级密码强度
isPassword('a12345678', {level: 3}); // false
isPassword('Aa12345678', {level: 3}); // true

// 3级密码强度,大小写字符仅计算1级强度
isPassword('Aa12345678', {level: 3, ignoreCase: true}); // false
isPassword('_Aa12345678', {level: 3, ignoreCase: true}); // true

// 仅支持 数字、字母、特殊字符,其他非法字符如中文字符是校验不通过的
isPassword('_Aa一二三45678', {level: 3, ignoreCase: true}); // false
isPassword(' _Aa12345678', {level: 3, ignoreCase: true}); // false
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
level number <optional>
2

密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符)

ignoreCase boolean <optional>
false

是否忽略大小写,为 ture 时,大小写字母视为一种字符

special string <optional>
"!@#$%^&*()-=_+[]\|{},./?<>~"

支持的特殊字符

Requires:
Returns:

值是否符合密码强度

Type
boolean

(static) isPostcode(value) → {boolean}

Description:
  • 检测值是否为邮政编码,6位数字

Source:
Since:
  • 1.1.0
Example
isPostcode('101111'); // true
isPostcode('123'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为邮政编码

Type
boolean

(static) isQQ(value) → {boolean}

Description:
  • 检测值是否为QQ号,非0开头,5至11位数字

Source:
Since:
  • 1.1.0
Example
isQQ('12345'); // true
isQQ('123'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为QQ号

Type
boolean

(static) isSocialCreditCode(value, optionsopt) → {boolean}

Description:
  • 检测值是否为统一社会信用代码,也叫三证合一组织代码。由18位数字和大写字母组成,不使用I、O、Z、S、V。

Source:
Since:
  • 1.1.0
See:
Example
isSocialCreditCode('91350100M000100Y43'); // true
isSocialCreditCode('91350100M000100Y4A'); // false

// 不校验校验位,长度和类型还是有校验的
isSocialCreditCode('91350100M000100Y4A', { checkCode: false }); // true
isSocialCreditCode('91350100M000100YIO', { checkCode: false }); // false
isSocialCreditCode('91350100M000100Y', { checkCode: false }); // false
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
checkCode boolean <optional>
true

是否校验最后一位校验码,如果为false,不校验校验位。

Returns:

值是否为统一社会信用代码

Type
boolean

(static) isSwiftCode(value) → {boolean}

Description:
  • 检测值是否为 Swift Code。8位或11位,前6位为大写字母,7-8位为大写字母或数字,9-11位为可选的大写字母或数字。

Source:
Since:
  • 4.9.0
See:
Example
isSwiftCode('DEUTDEFF'); // true
isSwiftCode('deutdeff'); // false

isSwiftCode('BKTWTWTP010'); // true
isSwiftCode('010BKTWTWTP'); // false

isSwiftCode('ICBKCNBJBJM'); // true
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为 Swift Code

Type
boolean

(static) isTWCard(value, optionsopt) → {boolean}

Description:
  • 检测值是否为台湾居民来往大陆通行证,俗称台胞证。

Source:
Since:
  • 4.0.0
See:
Example
isTWCard('12345678'); // true
isTWCard('07257456'); // true

// 一次性短期
isTWCard('F290299977'); // false

// 宽松模式,支持一次性短期通行证
isTWCard('F290299977', { loose: true }); // true
Parameters:
Name Type Attributes Description
value *

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
loose boolean <optional>
false

宽松模式。如果为true,表示支持一次性短期通行证

Returns:

是否为台湾居民来往大陆通行证

Type
boolean

(static) isTelephone(value) → {boolean}

Description:
  • 检测值是否为固定电话

Source:
Since:
  • 1.1.0
Example
isTelephone('22033212'); // true
isTelephone('13000000000'); // false

// 含区号
isTelephone('021-22033212'); // true

// 含区号和分机号
isTelephone('021-22033212-123'); // true
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为固定电话

Type
boolean

(static) isUrl(value) → {boolean}

Description:
  • 检测值是否为url

Source:
Since:
  • 3.4.0
See:
Example
isUrl(''); // false
isUrl('/foo/bar'); // false

isUrl('8.8.8.8'); // true
isUrl('example.com'); // true
isUrl('http://example.com'); // true
isUrl('https://example.com:8080'); // true
isUrl('https://www.example.com/test/123'); // true
isUrl('https://www.example.com/test/123?foo=bar'); // true
isUrl('https://www.example.com/test/123?foo=中文#id'); // true
isUrl('https://www.example.com/test/123?foo=中文#测试'); // true
isUrl('ftp://127.0.0.1:8080/测试.tar'); // true
isUrl('a.b'); // true
isUrl('a.b:8080'); // true
isUrl('p://a.b'); // true
isUrl('p://a.b:8888'); // true
isUrl('中文域名.中文后缀'); // true
isUrl('中文域名.cn'); // true
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为url

Type
boolean

(static) isValidNumber(value, strictopt) → {boolean}

Description:
  • 检测值是否为有效数值,支持隐式转换。如果返回 true ,表示可以通过 Number() 转为数字。

Source:
Since:
  • 4.17.2
Example
isValidNumber(null); // true
isValidNumber(true); // true
isValidNumber(''); // true
isValidNumber(1234); // true
isValidNumber('1234'); // true
isValidNumber(' 1234 '); // true
isValidNumber(' 1234 '); // true

isValidNumber(undefined); // false
isValidNumber('0.10.1'); // false

// 严格模式
isValidNumber(null, true); // false
isValidNumber(true, true); // false
isValidNumber('', true); // false
isValidNumber(1234, true); // true
isValidNumber('1234', true); // true
isValidNumber(' 1234 ', true); // true
isValidNumber(' 1234 ', true); // true
Parameters:
Name Type Attributes Default Description
value *

待检测的值

strict boolean <optional>
false

严格模式,如果为 true ,仅支持字符串和数字类型,不处理其他类型隐式转换,且空字符串返回 false 。

Returns:

值是否为有效数值

Type
boolean

(static) isVehicle(value) → {boolean}

Description:
  • 检测值是否为车牌号,支持新能源和非新能源车牌

Source:
Since:
  • 1.1.0
See:
Example
isVehicle('京L12345'); // true
isVehicle('京L1234学'); // true
isVehicle('BL1234警'); // true

// 新能源车牌
isVehicle('粤BD12345'); // true
isVehicle('粤BF12345'); // true
isVehicle('粤B12345D'); // true
isVehicle('粤B12345F'); // true
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为车牌号

Type
boolean

(static) isWX(value) → {boolean}

Description:
  • 检测值是否为微信号

Source:
Since:
  • 1.1.0
Example
isWX('a12345'); // true
isWX('123'); // false
Parameters:
Name Type Description
value *

要检测的值

Returns:

值是否为微信号

Type
boolean

(static) validatePassword(value, optionsopt) → {ValidatePasswordReturn}

Description:
  • 验证密码(数字、大小写字母、特殊字符、非法字符)

Source:
Since:
  • 3.7.0
See:
Example
validatePassword('a12345678');
// =>
{
  validated: true, // 验证结果,根据密码强度、是否包含非法字符得出
  level: 2, // 强度级别
  containes: {
    number: true, // 包含数字
    lowerCaseLetter: true, // 包含小写字母
    upperCaseLetter: false, // 包含大写字母
    specialCharacter: false, // 包含特殊字符
    unallowableCharacter: false // 包含非法字符
  }
}

validatePassword('a12345678', { level: 3 });
// =>
{
  validated: false,
  level: 2,
  containes: {
    number: true,
    lowerCaseLetter: true,
    upperCaseLetter: false,
    specialCharacter: false,
    unallowableCharacter: false
  }
}

validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true });
// =>
{
  validated: false,
  level: 3,
  containes: {
    number: true,
    lowerCaseLetter: true,
    upperCaseLetter: true,
    specialCharacter: true,
    unallowableCharacter: true
  }
}

// 自定义特殊字符
validatePassword('_Aa一二三45678', { level: 3, ignoreCase: true, special: '_一二三' });
// =>
{
  validated: true,
  level: 3,
  containes: {
    number: true,
    lowerCaseLetter: true,
    upperCaseLetter: true,
    specialCharacter: true,
    unallowableCharacter: false
  }
}
Parameters:
Name Type Attributes Description
value string

要检测的值

options Object <optional>

配置项

Properties
Name Type Attributes Default Description
level number <optional>
2

密码强度 1-包含一种字符 2-包含两种字符 3-包含三种字符。(大写字母、小写字母、数字、特殊字符)

ignoreCase boolean <optional>
false

是否忽略大小写,为 ture 时,大小写字母视为一种字符

special string <optional>
"!@#$%^&*()-=_+[]\|{},./?<>~"

支持的特殊字符

Returns:

验证结果

Type
ValidatePasswordReturn