Commit 313d9e80 by Lin

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

- 将输入字段包裹在新的容器中,以优化布局并添加错误信息显示。
- 实现电话号码的输入验证,提供实时反馈和错误提示。
- 更新输入处理逻辑,确保电话号码字段仅接受数字值。
1 parent 3a3e7a3b
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
<view class="required" v-if="i.required">*</view>{{ i.label }} <view class="required" v-if="i.required">*</view>{{ i.label }}
</view> </view>
<template v-if="i.type == 'input'"> <template v-if="i.type == 'input'">
<view class="input-wrap">
<input <input
:type="i.inputType" :type="i.inputType"
:maxlength="i.maxlength" :maxlength="i.maxlength"
...@@ -24,8 +25,11 @@ ...@@ -24,8 +25,11 @@
:placeholder="`请输入${i.label}`" :placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);" placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done" confirm-type="done"
@input="inputChange($event, i)"
@blur="inputBlur($event, i)" @blur="inputBlur($event, i)"
/> />
<view class="error-text" v-if="i.error && i.errorMsg">{{ i.errorMsg }}</view>
</view>
</template> </template>
<template v-else-if="i.type == 'date'"> <template v-else-if="i.type == 'date'">
<picker <picker
...@@ -66,6 +70,7 @@ ...@@ -66,6 +70,7 @@
<view class="required" v-if="i.required">*</view>{{ i.label }} <view class="required" v-if="i.required">*</view>{{ i.label }}
</view> </view>
<template v-if="i.type == 'input'"> <template v-if="i.type == 'input'">
<view class="input-wrap">
<input <input
:type="i.inputType" :type="i.inputType"
:maxlength="i.maxlength" :maxlength="i.maxlength"
...@@ -74,8 +79,11 @@ ...@@ -74,8 +79,11 @@
:placeholder="`请输入${i.label}`" :placeholder="`请输入${i.label}`"
placeholder-style="color:rgba(51, 51, 51, 0.2);" placeholder-style="color:rgba(51, 51, 51, 0.2);"
confirm-type="done" confirm-type="done"
@input="inputChange($event, i)"
@blur="inputBlur($event, i)" @blur="inputBlur($event, i)"
/> />
<view class="error-text" v-if="i.error && i.errorMsg">{{ i.errorMsg }}</view>
</view>
</template> </template>
<template v-else-if="i.type == 'date'"> <template v-else-if="i.type == 'date'">
<picker <picker
...@@ -367,7 +375,45 @@ export default { ...@@ -367,7 +375,45 @@ export default {
this.inviteDateTouched = true 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) { 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 const standNoVal = this.customerInfo.standNo
if (val.key === 'standNo' && standNoVal) { if (val.key === 'standNo' && standNoVal) {
this.customerInfo.standNo = formatVinInput(standNoVal) this.customerInfo.standNo = formatVinInput(standNoVal)
...@@ -406,6 +452,7 @@ export default { ...@@ -406,6 +452,7 @@ export default {
try { try {
this.customerForm.map(e => { this.customerForm.map(e => {
e.error = false e.error = false
e.errorMsg = ''
}) })
let { customerInfo, customerForm, optionalForm } = this, let { customerInfo, customerForm, optionalForm } = this,
{ shopMerchantId, sceneType } = uni.$storage.get("userInfo"), { shopMerchantId, sceneType } = uni.$storage.get("userInfo"),
...@@ -414,7 +461,10 @@ export default { ...@@ -414,7 +461,10 @@ export default {
merchantId: shopMerchantId, merchantId: shopMerchantId,
bussinessScene: sceneType == 0 ? 1 : 2 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 => { customerForm.map(e => {
params[e.key] = customerInfo[e.key] params[e.key] = customerInfo[e.key]
}) })
...@@ -500,7 +550,18 @@ export default { ...@@ -500,7 +550,18 @@ export default {
} catch (error) { } catch (error) {
const index = this.customerForm.findIndex(e => e.key == error) const index = this.customerForm.findIndex(e => e.key == error)
if (index !== -1) { 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() uni.hideLoading()
} }
...@@ -581,11 +642,25 @@ export default { ...@@ -581,11 +642,25 @@ export default {
justify-content: space-between; justify-content: space-between;
.form-item { .form-item {
width: toRpx(934); width: toRpx(934);
height: toRpx(80); min-height: toRpx(80);
font-size: toRpx(28); 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 { .label {
width: toRpx(192); width: toRpx(192);
height: toRpx(76);
color: #292d33; color: #292d33;
justify-content: center; justify-content: center;
.required { .required {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!