-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (30 loc) · 1.21 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// script.js
document.addEventListener('DOMContentLoaded', () => {
const inputs = document.querySelectorAll('.otp-input');
const messageContainer = document.querySelector('.otp-message');
let otpValue = '';
inputs.forEach((input, index) => {
input.addEventListener('input', () => {
const value = input.value;
// Remove non-numeric characters
if (value.match(/[^0-9]/)) {
input.value = value.replace(/[^0-9]/g, '');
}
otpValue = Array.from(inputs).map(input => input.value).join('');
if (index < inputs.length - 1 && value) {
inputs[index + 1].focus();
}
if (index > 0 && !value) {
inputs[index - 1].focus();
}
if (otpValue.length === inputs.length) {
if (otpValue === '00000') {
inputs.forEach(input => input.classList.add('error'));
messageContainer.textContent = 'The entered number is incorrect';
} else {
alert(`your number is: ${otpValue}`);
}
}
});
});
});