Commit 313d9e80 by Lin

feat(AddFiles):增强电话号码字段的输入处理和错误提示功能

- 将输入字段包裹在新的容器中,以优化布局并添加错误信息显示。
- 实现电话号码的输入验证,提供实时反馈和错误提示。
- 更新输入处理逻辑,确保电话号码字段仅接受数字值。
1 parent 3a3e7a3b
......@@ -16,16 +16,20 @@
<view class="required" v-if="i.required">*</view>{{ i.label }}
</view>
<template v-if="i.type == 'input'">
<input
:type="i.inputType"
:maxlength="i.maxlength"
v-model="customerInfo[i.valueKey]"
:class="['input', { error: i.error }]"
:placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done"
@blur="inputBlur($event, i)"
/>
<view class="input-wrap">
<input
:type="i.inputType"
:maxlength="i.maxlength"
v-model="customerInfo[i.valueKey]"
:class="['input', { error: i.error }]"
:placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done"
@input="inputChange($event, i)"
@blur="inputBlur($event, i)"
/>
<view class="error-text" v-if="i.error && i.errorMsg">{{ i.errorMsg }}</view>
</view>
</template>
<template v-else-if="i.type == 'date'">
<picker
......@@ -66,16 +70,20 @@
<view class="required" v-if="i.required">*</view>{{ i.label }}
</view>
<template v-if="i.type == 'input'">
<input
:type="i.inputType"
:maxlength="i.maxlength"
v-model="customerInfo[i.valueKey]"
:class="['input', { error: i.error }]"
:placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done"
@blur="inputBlur($event, i)"
/>
<view class="input-wrap">
<input
:type="i.inputType"
:maxlength="i.maxlength"
v-model="customerInfo[i.valueKey]"
:class="['input', { error: i.error }]"
:placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done"
@input="inputChange($event, i)"
@blur="inputBlur($event, i)"
/>
<view class="error-text" v-if="i.error && i.errorMsg">{{ i.errorMsg }}</view>
</view>
</template>
<template v-else-if="i.type == 'date'">
<picker
......@@ -367,7 +375,45 @@ export default {
this.inviteDateTouched = true
}
},
getPhoneErrorMsg (phone) {
if (!phone) return '请输入手机号'
if (!/^\d+$/.test(phone)) return '手机号只能包含数字'
if (phone.length < 11) return '手机号应为11位'
if (!phoneRex.test(phone)) return '请输入正确的手机号'
return ''
},
setPhoneFieldError (field, phone) {
const errorMsg = this.getPhoneErrorMsg(phone)
field.error = !!errorMsg
field.errorMsg = errorMsg
return !errorMsg
},
inputChange (e, field) {
if (field.key !== 'customerPhone') return
const value = (e.detail.value || '').replace(/\D/g, '').slice(0, 11)
this.customerInfo.customerPhone = value
if (field.error) {
this.setPhoneFieldError(field, value)
}
},
inputBlur (e, val) {
if (val.key === 'customerPhone') {
const phone = (this.customerInfo.customerPhone || '').replace(/\D/g, '')
this.customerInfo.customerPhone = phone
if (phone) {
const valid = this.setPhoneFieldError(val, phone)
if (!valid) {
uni.showToast({
title: val.errorMsg,
icon: 'none'
})
}
} else {
val.error = false
val.errorMsg = ''
}
return
}
const standNoVal = this.customerInfo.standNo
if (val.key === 'standNo' && standNoVal) {
this.customerInfo.standNo = formatVinInput(standNoVal)
......@@ -406,6 +452,7 @@ export default {
try {
this.customerForm.map(e => {
e.error = false
e.errorMsg = ''
})
let { customerInfo, customerForm, optionalForm } = this,
{ shopMerchantId, sceneType } = uni.$storage.get("userInfo"),
......@@ -414,7 +461,10 @@ export default {
merchantId: shopMerchantId,
bussinessScene: sceneType == 0 ? 1 : 2
}
if (!phoneRex.test(customerInfo.customerPhone)) throw 'customerPhone'
const phoneField = customerForm.find(e => e.key === 'customerPhone')
const phone = (customerInfo.customerPhone || '').replace(/\D/g, '')
customerInfo.customerPhone = phone
if (phoneField && !this.setPhoneFieldError(phoneField, phone)) throw 'customerPhone'
customerForm.map(e => {
params[e.key] = customerInfo[e.key]
})
......@@ -500,7 +550,18 @@ export default {
} catch (error) {
const index = this.customerForm.findIndex(e => e.key == error)
if (index !== -1) {
this.customerForm[index].error = true
const field = this.customerForm[index]
if (error === 'customerPhone') {
this.setPhoneFieldError(field, this.customerInfo.customerPhone)
} else {
field.error = true
}
if (field.errorMsg) {
uni.showToast({
title: field.errorMsg,
icon: 'none'
})
}
}
uni.hideLoading()
}
......@@ -581,11 +642,25 @@ export default {
justify-content: space-between;
.form-item {
width: toRpx(934);
height: toRpx(80);
min-height: toRpx(80);
font-size: toRpx(28);
align-items: flex-start;
}
.input-wrap {
width: toRpx(738);
.input {
width: 100%;
}
.error-text {
margin-top: toRpx(8);
font-size: toRpx(24);
line-height: toRpx(32);
color: #f24724;
}
}
.label {
width: toRpx(192);
height: toRpx(76);
color: #292d33;
justify-content: center;
.required {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!