<!-- Header -->
<h1>Enter The Pin</h1>
<!-- Form -->
<form id="form">
<!-- Inputs -->
<input type="text" maxlength="1" class="input" />
<input type="text" maxlength="1" class="input" />
<input type="text" maxlength="1" class="input" />
<input type="text" maxlength="1" class="input" />
<input type="text" maxlength="1" class="input" />
<input type="text" maxlength="1" class="input" />
</form>
* {
box-sizing: border-box;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Arial, Helvetica, sans-serif;
overflow: hidden;
color: white;
height: 100vh;
background-color: hsl(218, 17%, 9%);
background-color: #afb8c8;
}
/* Container holding all elements, allowing scrolling if needed */
#container {
width: 100%;
overflow: auto;
}
/* Form container */
#form {
display: flex;
}
/* Input field */
.input {
width: 45px;
padding: 7px;
text-align: center;
font-size: 2rem;
outline: 0;
margin: 0 5px;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.5);
background-color: rgba(255, 255, 255, 0.5);
}
// inputs
const inputs = document.querySelectorAll('.input');
// Loop through inputs
inputs.forEach((input, index) => {
// input field list
input.addEventListener('input', (e) => {
e.target.value = e.target.value.replace(/[^0-9]/g, ''); // Allow only numbers
if (e.target.value && index < inputs.length - 1) {
inputs[index + 1].focus(); // Move to next input
}
});
// Move to previous input on backspace
input.addEventListener('keydown', (e) => {
if (e.key === 'Backspace' && index > 0 && !e.target.value) {
inputs[index - 1].focus(); // Move to previous input on backspace
}
});
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.