uView has some built-in verification rules, such as whether it is a mobile phone number, email number, URL, etc.
These rule methods are mounted $u.test
below, such as verifying whether it is a mobile phone number: $u.test.mobile('13888889999')
, if the verification is passed, return true
, otherwise returnfalse
#Whether to verify the code
#code(value, len = 6)
Verify whether the verification code (required to be a number), return true
or false
.
value
<String> Verification code stringlen
<Number> Verification code length, default is 6
console.log(uni.$u.test.code('4567', 4));
copy
#Whether it is an array
#array(array)
Check whether it is an array and return true
or false
.
array
<Array> array
console.log(uni.$u.test.array([1, 2, 3]));
copy
#Whether it is a Json string
#jsonString(json)
Check whether it is an array and return true
or false
.
json
<Json> Json string
Note: Please pay attention to the requirements of json string:
- The whole is a string
- Properties within the string object need to be
""
enclosed in double quotes
console.log(uni.$u.test.jsonString('{"a": 1}'));
copy
#Is it an object?
#object(object)
Check whether it is an array and return true
or false
.
object
<Object> object
console.log(uni.$u.test.object({a: 1}));
copy
#Whether the email address is
#email(email)
Check whether the mailbox number is, return true
or false
.
email
<String> string
console.log(uni.$u.test.email('[email protected]'));
copy
#whether mobile phone number
#mobile(mobile)
Verify whether it is a mobile phone number and return true
or false
.
mobile
<String> string
console.log(uni.$u.test.mobile('13845678900'));
copy
#Whether it is URL
#url(url)
Verify whether the URL is linked and return true
or false
.
url
<String> string
console.log(uni.$u.test.url('http://www.uviewui.com'));
copy
#Is it empty?
The "empty" referred to here includes the following situations:
- Value is
undefined
(a type), not a string"undefined"
- The string length is 0, which is the empty string
- Value is
false
(Boolean type), not string"false"
- value is a numeric value
0
(not a string"0"
), orNaN
- Value is
null
, an empty object{}
, or an array of length 0
#isEmpty(value)
Check whether the check value is empty, return true
or false
.
This method is equivalent to empty
name, but for more semantics, isEmpty
name is recommended.
value
<any> string
console.log(uni.$u.test.isEmpty(false));
copy
#Is it a common date?
To verify whether a string is a date, return true
or false
, the following behavior is correct:
2020-02-10
、2020-02-10 08:32:10
、2020/02/10 3:10
、2020/02/10 03:10
、2020/02-10 3:10
The following is an error:
2020年02月10日
、2020-02-10 25:32
In general, the year, month and day can be separated by "/" or "-" (cannot be separated by Chinese characters), and the hours, minutes and seconds can be separated by ":". The value cannot exceed the range. For example, the month cannot be 13, then check Success, otherwise failure.
#date(date)
date
<String> date string
console.log(uni.$u.test.date('2020-02-10 08:32:10'));
copy
#Whether it is a decimal value
Integers, decimals, negative numbers, with thousandths (2,359.08), etc. can pass the test and return true
or false
.
#number(number)
number
<String> number
console.log(uni.$u.test.number('2020'));
copy
#Is it an integer?
Only if all characters are 0-9
between, the verification passes, and the result is returned true
or false
.
#digits(number)
number
<String> number
console.log(uni.$u.test.digits('2020'));
copy
#Is it an ID number?
The ID number, including the type ending in "X", can pass the verification, and the result is returned true
or false
.
#idCard(idCard)
idCard
<String> ID number
console.log(uni.$u.test.idCard('110101199003070134'));
copy
#Whether the license plate number
The old license plate number and the new energy type license plate number can be verified, and the result is returned true
or false
.
# carNo(carNo)
carNo
<String> License plate number
console.log(uni.$u.test.carNo('京A88888'));
copy
#Whether the amount
Up to two decimal places, with thousandths possible, and the result is returned true
or false
.
#amount(amount)
amount
<String> Amount string
console.log(uni.$u.test.amount('3,233.08'));
copy
#Whether Chinese characters
It can be a single Chinese character, or a string composed of Chinese characters, and the result is true
returned false
.
# chinese(zh)
zh
<String> Chinese string
console.log(uni.$u.test.chinese('更上一层楼'));
copy
#Whether it is a letter
It can only be characters between "az" or "AZ", and the result returns true
or false
.
# letter(s)
en
<String> Letter string
console.log(uni.$u.test.letter('uView'));
copy
#Whether it is a letter or a number
It can only be letters or numbers, and the result is returned true
or false
.
#enOrNum(str)
str
<String> alphanumeric string
console.log(uni.$u.test.enOrNum('uView'));
copy
#Whether it contains a certain value
Whether the string contains a certain substring, case-sensitive, the result is returned true
or false
.
#contains(str, subStr)
str
<String> stringsubStr
<String> substring
console.log(uni.$u.test.contains('uView', 'View'));
copy
#Whether the value is within a certain range
If 30 is within the range of "29-35" but not within the range of "25-28", the result will be returned true
or false
.
#range(number, range)
number
<Number> valuerange
<Array> such as "[25-35]"
console.log(uni.$u.test.range(35, [30, 34]));
copy
#Whether the string length is within a certain range
For example, the length of "abc" is 3 and the range is in the interval "2-5". The result returns true
or false
.
#rangeLength(str, range)
str
<String> valuerange
<Array> such as "[25, 35]"
console.log(uni.$u.test.rangeLength('abc', [3, 10]));