Skip to content

Instantly share code, notes, and snippets.

@ibahas
Created February 1, 2024 14:23
Show Gist options
  • Save ibahas/e30e78f0c01de1020961a3e24d7eb76c to your computer and use it in GitHub Desktop.
Save ibahas/e30e78f0c01de1020961a3e24d7eb76c to your computer and use it in GitHub Desktop.
Verification Code - Pure HTML, CSS, JS
<div id="wrapper">
<div class="verify-code">
<input class="form-control code-1" tabindex="0" type="number" maxlength="1" size="1" min="0" max="9" />
<input class="form-control code-2" tabindex="1" type="number" maxlength="1" size="1" min="0" max="9" />
<input class="form-control code-3" tabindex="2" type="number" maxlength="1" size="1" min="0" max="9" />
<input class="form-control code-4" tabindex="3" type="number" maxlength="1" size="1" min="0" max="9" />
</div>
<h1></h1>
</div>
var codes = new Array(4);
var re = /^\d+$/;
var inputs = [].slice.call(document.querySelectorAll('.verify-code input'));
inputs.map(function (input, index) {
input.addEventListener('input', function (e) {
this.value = this.value.slice(0, this.maxLength);
});
input.addEventListener('keyup', function (e) {
if (re.test(input.value)) {
codes[index] = input.value;
var nextInput = inputs[index + 1];
if (nextInput) {
nextInput.focus();
nextInput.select();
} else {
inputs[0].focus();
inputs[0].select();
}
}
document.querySelector('h1').innerHTML = codes.join('');
});
input.addEventListener('click', function (e) {
e.target.select();
});
});
#wrapper {
margin: 0 auto;
width: 260px;
}
input {
width: 30px;
padding: 10px;
margin: 0 5px;
border: 1px solid #ccc;
}
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[type='number'] {
-moz-appearance: textfield;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment