uView test 规则校验

uView内置了一些校验规则,如是否手机号,邮箱号,URL等
这些规则方法,挂载在$u.test下面,如验证是否手机号:$u.test.mobile('13888889999'),如果验证通过,返回true,否则返回false

#是否验证码

#code(value, len = 6)

校验是否验证码(要求为数字),返回true或者false

  • value <String> 验证码字符串
  • len <Number> 验证码长度,默认为6
console.log(uni.$u.test.code('4567', 4));

copy

#是否数组

#array(array)

校验是否数组,返回true或者false

  • array <Array> 数组
console.log(uni.$u.test.array([1, 2, 3]));

copy

#是否Json字符串

#jsonString(json)

校验是否数组,返回true或者false

  • json <Json> Json字符串

注意:请留意json字符串的要求:

  1. 整体为一个字符串
  2. 字符串对象内的属性需要用""双引号包含
console.log(uni.$u.test.jsonString('{"a": 1}'));

copy

#是否对象

#object(object)

校验是否数组,返回true或者false

  • object <Object> 对象
console.log(uni.$u.test.object({a: 1}));

copy

#是否邮箱号

#email(email)

校验是否邮箱号,返回true或者false

  • email <String> 字符串
console.log(uni.$u.test.email('[email protected]'));

copy

#是否手机号

#mobile(mobile)

校验是否手机号,返回true或者false

  • mobile <String> 字符串
console.log(uni.$u.test.mobile('13845678900'));

copy

#是否URL

#url(url)

校验是否URL链接,返回true或者false

  • url <String> 字符串
console.log(uni.$u.test.url('http://www.uviewui.com'));

copy

#是否为空

这里指的“空”,包含如下几种情况:

  • 值为undefined(一种类型),非字符串"undefined"
  • 字符串长度为0,也即空字符串
  • 值为false(布尔类型),非字符串"false"
  • 值为数值0(非字符串"0"),或者NaN
  • 值为null,空对象{},或者长度为0的数组
#isEmpty(value)

校验值是否为空,返回true或者false
此方法等同于empty名称,但是为了更语义化,推荐用isEmpty名称。

  • value <any> 字符串
console.log(uni.$u.test.isEmpty(false));

copy

#是否普通日期

验证一个字符串是否日期,返回true或者false,如下行为正确:

  • 2020-02-102020-02-10 08:32:102020/02/10 3:102020/02/10 03:102020/02-10 3:10

如下为错误:

  • 2020年02月10日2020-02-10 25:32

总的来说,年月日之间可以用"/"或者"-"分隔(不能用中文分隔),时分秒之间用":"分隔,数值不能超出范围,如月份不能为13,则检验成功,否则失败。

#date(date)
  • date <String> 日期字符串
console.log(uni.$u.test.date('2020-02-10 08:32:10'));

copy

#是否十进制数值

整数,小数,负数,带千分位数(2,359.08)等可以检验通过,返回true或者false

#number(number)
  • number <String> 数字
console.log(uni.$u.test.number('2020'));

copy

#是否整数

所有字符都在0-9之间,才校验通过,结果返回true或者false

#digits(number)
  • number <String> 数字
console.log(uni.$u.test.digits('2020'));

copy

#是否身份证号

身份证号,包括尾数为"X"的类型,可以校验通过,结果返回true或者false

#idCard(idCard)
  • idCard <String> 身份证号
console.log(uni.$u.test.idCard('110101199003070134'));

copy

#是否车牌号

可以校验旧车牌号和新能源类型车牌号,结果返回true或者false

#carNo(carNo)
  • carNo <String> 车牌号
console.log(uni.$u.test.carNo('京A88888'));

copy

#是否金额

最多两位小数,可以带千分位,结果返回true或者false

#amount(amount)
  • amount <String> 金额字符串
console.log(uni.$u.test.amount('3,233.08'));

copy

#是否汉字

可以为单个汉字,或者汉字组成的字符串,结果返回true或者false

#chinese(zh)
  • zh <String> 中文字符串
console.log(uni.$u.test.chinese('更上一层楼'));

copy

#是否字母

只能为"a-z"或者"A-Z"之间的字符,结果返回true或者false

#letter(en)
  • en <String> 字母串
console.log(uni.$u.test.letter('uView'));

copy

#是否字母或者数字

只能是字母或者数字,结果返回true或者false

#enOrNum(str)
  • str <String> 字母或者数字字符串
console.log(uni.$u.test.enOrNum('uView'));

copy

#是否包含某个值

字符串中是否包含某一个子字符串,区分大小写,结果返回true或者false

#contains(str, subStr)
  • str <String> 字符串
  • subStr <String> 子字符串
console.log(uni.$u.test.contains('uView', 'View'));

copy

#数值是否在某个范围内

如30在"29-35"这个范围内,不在"25-28"这个范围内,结果返回true或者false

#range(number, range)
  • number <Number> 数值
  • range <Array> 如"[25-35]"
console.log(uni.$u.test.range(35, [30, 34]));

copy

#字符串长度是否在某个范围内

如"abc"长度为3,范围在"2-5"这个区间,结果返回true或者false

#rangeLength(str, range)
  • str <String> 数值
  • range <Array> 如"[25, 35]"
console.log(uni.$u.test.rangeLength('abc', [3, 10]));

猜你喜欢

转载自blog.csdn.net/m0_72196169/article/details/135464412