index.vue
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--
* @Date: 2022-09-02 10:46:03
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-09-02 11:36:29
* @FilePath: /data-table/src/components/PhoneField/index.vue
* @Description: 文件描述
-->
<template>
<div class="phone-field-page">
<div class="label">{{ item.label }}<span v-if="item.required"> *</span></div>
<van-field v-model="item.value" :name="item.name" type="digit" maxlength="11" :placeholder="item.placeholder"
:rules="rules" :required="item.required" :readonly="readonly" @touchstart.stop="show = true"></van-field>
<van-number-keyboard v-model="item.value" :show="show" :maxlength="11" @blur="show = false" />
</div>
</template>
<script setup>
import { wxInfo } from '@/utils/tools'
const props = defineProps({
item: Object
});
// web端判断
const readonly = computed(() => wxInfo().isMobile)
// 校验函数返回 true 表示校验通过,false 表示不通过
const validator = (val) => {
if (!props.item.required) { // 非必填
return true
} else {
return /1\d{10}/.test(val);
}
};
// 错误提示文案
const validatorMessage = (val, rule) => {
if (!val) {
return '手机号码不能为空';
} else if (!/1\d{10}/.test(val)) {
return '请输入正确手机号码';
}
}
const rules = [{ validator, message: validatorMessage }]
const show = ref(false);
</script>
<style lang="less" scoped>
.phone-field-page {
.label {
padding: 1rem 1rem 0 1rem;
font-size: 0.9rem;
font-weight: bold;
span {
color: red;
}
}
}
</style>