test — Type Check
17:43Description
$u.test is a set of pure predicate methods covering type checks, number and string formats, ID and phone numbers, media URLs, gradients and empty values. Every method returns a boolean.
Examples
Type checking
string, number, array, object and func each check one value, and the result is shown as a tag.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in typeList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const typeList: TestItem[] = [
{ desc: '$u.test.string("hello")', result: $u.test.string('hello'), last: false },
{ desc: '$u.test.number("123")', result: $u.test.number('123'), last: false },
{ desc: '$u.test.array([1,2,3])', result: $u.test.array([1, 2, 3]), last: false },
{ desc: '$u.test.object({})', result: $u.test.object({}), last: false },
{ desc: '$u.test.func(() => {})', result: $u.test.func((): void => {}), last: true }
];
</script>Number validation
Compares how number (decimals and a sign allowed), digits (digits only) and range (inclusive bounds) treat valid and invalid input.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in numberList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const numberList: TestItem[] = [
{ desc: '$u.test.number("123")', result: $u.test.number('123'), last: false },
{ desc: '$u.test.number("-12.34")', result: $u.test.number('-12.34'), last: false },
{ desc: '$u.test.number("12a")', result: $u.test.number('12a'), last: false },
{ desc: '$u.test.digits("123")', result: $u.test.digits('123'), last: false },
{ desc: '$u.test.digits("12.3")', result: $u.test.digits('12.3'), last: false },
{ desc: '$u.test.range(5, 1, 10)', result: $u.test.range(5, 1, 10), last: false },
{ desc: '$u.test.range(15, 1, 10)', result: $u.test.range(15, 1, 10), last: true }
];
</script>String validation
Covers email, mobile number, URL, Chinese characters, letters, substring containment and length range; letter('abc123') returns false.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in stringList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const stringList: TestItem[] = [
{ desc: '$u.test.email("user@example.com")', result: $u.test.email('user@example.com'), last: false },
{ desc: '$u.test.mobile("13800138000")', result: $u.test.mobile('13800138000'), last: false },
{ desc: '$u.test.url("https://www.example.com")', result: $u.test.url('https://www.example.com'), last: false },
{ desc: '$u.test.chinese("中文")', result: $u.test.chinese('中文'), last: false },
{ desc: '$u.test.letter("abc")', result: $u.test.letter('abc'), last: false },
{ desc: '$u.test.letter("abc123")', result: $u.test.letter('abc123'), last: false },
{ desc: '$u.test.contains("hello world", "world")', result: $u.test.contains('hello world', 'world'), last: false },
{ desc: '$u.test.rangeLength("hello", 2, 10)', result: $u.test.rangeLength('hello', 2, 10), last: true }
];
</script>ID & number validation
idCard checks 18-digit ID numbers, landline checks landline numbers and amount checks money format; input of the wrong length or format returns false.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in regexList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const regexList: TestItem[] = [
{ desc: '$u.test.idCard("110101199003077777")', result: $u.test.idCard('110101199003077777'), last: false },
{ desc: '$u.test.idCard("12345")', result: $u.test.idCard('12345'), last: false },
{ desc: '$u.test.landline("010-12345678")', result: $u.test.landline('010-12345678'), last: false },
{ desc: '$u.test.landline("010-1234")', result: $u.test.landline('010-1234'), last: false },
{ desc: '$u.test.amount("1,234.56")', result: $u.test.amount('1,234.56'), last: false },
{ desc: '$u.test.amount("1234.567")', result: $u.test.amount('1234.567'), last: false },
{ desc: '$u.test.amount("0.5")', result: $u.test.amount('0.5'), last: true }
];
</script>Structure & type checks
null does not treat an empty string as null, the second code argument is the digit count (null means 6), and enOrNum allows an empty string but rejects hyphens.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in structList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const structList: TestItem[] = [
// null 只判断 null/undefined,空串不算
{ desc: '$u.test.null(null)', result: $u.test.null(null), last: false },
{ desc: '$u.test.null("")', result: $u.test.null(''), last: false },
// code 第二位为位数,null 时按 6 位
{ desc: '$u.test.code("123456", null)', result: $u.test.code('123456', null), last: false },
{ desc: '$u.test.code("1234", 4)', result: $u.test.code('1234', 4), last: false },
{ desc: '$u.test.code("123", null)', result: $u.test.code('123', null), last: false },
// enOrNum 允许空串
{ desc: '$u.test.enOrNum("abc123")', result: $u.test.enOrNum('abc123'), last: false },
{ desc: '$u.test.enOrNum("abc-123")', result: $u.test.enOrNum('abc-123'), last: false },
{ desc: '$u.test.enOrNum("")', result: $u.test.enOrNum(''), last: true }
];
</script>Media & gradient
image and video match by file extension, and image ignores the query string; gradientColor only accepts values starting with linear- / radial- / conic-gradient(.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in mediaList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
const mediaList: TestItem[] = [
// image 按扩展名判断,查询串不参与
{ desc: '$u.test.image("https://a.com/x.png?v=1")', result: $u.test.image('https://a.com/x.png?v=1'), last: false },
{ desc: '$u.test.image("a.pdf")', result: $u.test.image('a.pdf'), last: false },
{ desc: '$u.test.video("a.mp4")', result: $u.test.video('a.mp4'), last: false },
{ desc: '$u.test.video("a.m3u8")', result: $u.test.video('a.m3u8'), last: false },
{ desc: '$u.test.video("a.txt")', result: $u.test.video('a.txt'), last: false },
// gradientColor 需带 linear-/radial-/conic-gradient(
{ desc: '$u.test.gradientColor("linear-gradient(90deg, #fff, #000)")', result: $u.test.gradientColor('linear-gradient(90deg, #fff, #000)'), last: false },
{ desc: '$u.test.gradientColor("#fff")', result: $u.test.gradientColor('#fff'), last: true }
];
</script>Empty value checking
empty treats null, an empty string, an empty array, 0 and false as empty, while a non-empty string returns false.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in emptyList"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag size="mini" :type="item.result ? 'success' : 'error'" :text="item.result ? 'true' : 'false'" />
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type TestItem = {
desc: string;
result: boolean;
last: boolean;
};
// null / 空串 / 空数组 / 0 / NaN / false / 空对象都算空
const emptyList: TestItem[] = [
{ desc: '$u.test.empty(null)', result: $u.test.empty(null), last: false },
{ desc: '$u.test.empty("")', result: $u.test.empty(''), last: false },
{ desc: '$u.test.empty([])', result: $u.test.empty([] as any[]), last: false },
{ desc: '$u.test.empty(0)', result: $u.test.empty(0), last: false },
{ desc: '$u.test.empty("hello")', result: $u.test.empty('hello'), last: true }
];
</script>API
string
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is a string |
number
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is a valid number (decimals and scientific notation supported) |
array
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is an array |
object
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is a plain object (not null, not an array) |
func
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is a function |
digits
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value consists only of 0-9 digits |
range
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The number to check | number | - | - |
| min | Lower bound (inclusive) | number | - | - |
| max | Upper bound (inclusive) | number | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value falls within the inclusive [min, max] range |
email
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string matches an email format |
mobile
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string is an 11-digit mainland mobile number |
url
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string is an http(s) URL |
chinese
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string consists only of Chinese characters |
letter
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string consists only of English letters; an empty string returns true |
contains
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to search in | string | - | - |
| param | The substring to find | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string contains the given substring |
rangeLength
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
| min | Minimum length (inclusive) | number | - | - |
| max | Maximum length (inclusive) | number | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string length falls within the inclusive [min, max] range |
idCard
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string is an 18-digit ID card number |
landline
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string is a landline number in area code-number form, optionally with an extension |
amount
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string is a valid amount (thousands separators and up to two decimals) |
null
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | null | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is null / undefined; on App only null is checked |
code
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
| len | Number of digits; null means 6 | number | null | null | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string consists of exactly len digits |
enOrNum
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The string to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the string consists only of English letters or digits; an empty string returns true |
image
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The URL to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the URL points to an image, judged by extension with the query string ignored |
video
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The URL to check | string | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the URL points to a video, judged by extension |
gradientColor
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The style value to check | string | null | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is a linear / radial / conic gradient |
empty
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| value | The value to check | any | null | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the value is empty (null, empty string, 0, NaN, false, empty array or empty object) |
Notes
null('')returnsfalse: an empty string does not count asnull. On App,nullonly checksnull; on other platforms it also checksundefined.letter('')andenOrNum('')returntrue, since both predicates allow an empty string.- When the second argument of
codeisnull, 6 digits are expected;lenmust be a non-negative integer. imagestrips the query string after?before checking, whilevideodoes not.