fix: 验证码重复输入/限制输入数字

This commit is contained in:
zhangyh
2025-09-23 16:52:50 +08:00
parent 55ca3e5462
commit 8f3fe77a27

View File

@@ -4,9 +4,11 @@
<input v-for="(c, index) in getCtData" :key="index" <input v-for="(c, index) in getCtData" :key="index"
type="text" type="text"
v-model="getCtData[index]" ref="input" v-model="getCtData[index]" ref="input"
inputmode="numeric"
pattern="[0-9]*" pattern="[0-9]*"
@input="e => {onInput(e.target.value, index)}" @input="e => {onInput(e.target.value, index)}"
@keydown.delete="e=>{onKeydown(e.target.value, index)}" @keydown="e => {onKeydown(e, index)}"
@keypress="e => {onKeypress(e)}"
@focus="onFocus" @focus="onFocus"
:disabled="loading" :disabled="loading"
> >
@@ -55,6 +57,7 @@ export default defineComponent({
}, },
methods: { methods: {
onInput(val, index) { onInput(val, index) {
console.log('input',val,index)
// val = val.replace(/[^0-9]/g, ''); // val = val.replace(/[^0-9]/g, '');
val = String(val).replace(/\D/g, ''); val = String(val).replace(/\D/g, '');
this.getCtData[index] = val this.getCtData[index] = val
@@ -80,14 +83,29 @@ export default defineComponent({
index = (index + this.ctSize) % this.ctSize; index = (index + this.ctSize) % this.ctSize;
this.$refs.input[index].focus(); this.$refs.input[index].focus();
}, },
onKeydown(val, index) { onKeypress(e) {
if (val === '') { // 只允许输入数字0-9
// 删除上一个input里的值并对其focus。 const char = String.fromCharCode(e.which);
if (index > 0) { if (!/[0-9]/.test(char)) {
this.getCtData[index - 1] = ''; e.preventDefault();
this.$refs.input[index - 1].focus(); }
},
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() { sendCaptcha() {