From 8f3fe77a277111fd0773fa1669d8416a64fff462 Mon Sep 17 00:00:00 2001 From: zhangyh Date: Tue, 23 Sep 2025 16:52:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=AA=8C=E8=AF=81=E7=A0=81=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E8=BE=93=E5=85=A5/=E9=99=90=E5=88=B6=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LoginPage/verificationCodeInput.vue | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/component/LoginPage/verificationCodeInput.vue b/src/component/LoginPage/verificationCodeInput.vue index 1582ec65..2a18a5f2 100644 --- a/src/component/LoginPage/verificationCodeInput.vue +++ b/src/component/LoginPage/verificationCodeInput.vue @@ -4,9 +4,11 @@ @@ -55,6 +57,7 @@ export default defineComponent({ }, methods: { onInput(val, index) { + console.log('input',val,index) // val = val.replace(/[^0-9]/g, ''); val = String(val).replace(/\D/g, ''); this.getCtData[index] = val @@ -80,14 +83,29 @@ export default defineComponent({ index = (index + this.ctSize) % this.ctSize; this.$refs.input[index].focus(); }, - onKeydown(val, index) { - if (val === '') { - // 删除上一个input里的值,并对其focus。 - if (index > 0) { - this.getCtData[index - 1] = ''; - this.$refs.input[index - 1].focus(); + onKeypress(e) { + // 只允许输入数字0-9 + const char = String.fromCharCode(e.which); + if (!/[0-9]/.test(char)) { + e.preventDefault(); + } + }, + onKeydown(e, index) { + // 处理删除键 + if (e.key === 'Backspace' || e.key === 'Delete') { + const val = e.target.value; + if (val === '') { + // 删除上一个input里的值,并对其focus。 + if (index > 0) { + this.getCtData[index - 1] = ''; + this.$refs.input[index - 1].focus(); + } } } + // 阻止其他非数字字符 + else if (e.key && !/[0-9]/.test(e.key) && !['Backspace', 'Delete', 'Tab', 'Enter', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(e.key)) { + e.preventDefault(); + } }, sendCaptcha() {