Skip to content

Commit abfa87c

Browse files
committed
Fix to support alphanumeric characters based on input type
1 parent 69790f7 commit abfa87c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

resources/views/components/otp-input.blade.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
handleInput(e, i) {
4747
const input = e.target;
4848
49-
// Only allow numeric characters and limit to one character
50-
input.value = input.value.replace(/\D/g, '').substring(0, 1);
49+
// If type is number only allow numeric characters and limit to one character
50+
input.value = (this.type === 'number')
51+
? input.value.replace(/\D/g, '').substring(0, 1)
52+
: input.value.substring(0, 1);
5153
5254
this.state = Array.from({ length: this.length }).map((element, idx) => {
5355
return this.$refs[`otp_${idx + 1}`].value || '';
@@ -65,22 +67,27 @@
6567
}
6668
},
6769
handlePaste(e) {
68-
// Get the pasted data, filter only numeric characters, and limit it to the maximum length of inputs
69-
const paste = e.clipboardData.getData('text').replace(/\D/g, '').substring(0, this.length);
70-
const inputs = Array.from(Array(this.length));
70+
// Get the pasted data, if type is number filter only numeric characters, and limit it to the maximum length of inputs
71+
const paste = (this.type === 'number')
72+
? e.clipboardData.getData('text').replace(/\D/g, '').substring(0, this.length)
73+
: e.clipboardData.getData('text');
7174
72-
@this.set('{{ $statePath }}', paste);
75+
const inputs = Array.from(Array(this.length));
7376
7477
inputs.forEach((element, idx) => {
7578
if (paste[idx]) {
7679
this.$refs[`otp_${idx + 1}`].value = paste[idx];
7780
}
7881
});
7982
83+
const focusInputNumber = (paste.length < this.length) ? paste.length+1 : this.length;
84+
85+
this.$nextTick(() => {
86+
this.$refs[`otp_${focusInputNumber}`].focus();
87+
});
88+
8089
if (paste.length === this.length) {
81-
this.$nextTick(() => {
82-
this.$refs[`otp_${this.length}`].focus();
83-
});
90+
@this.set('{{ $statePath }}', paste);
8491
}
8592
8693
e.preventDefault();

0 commit comments

Comments
 (0)