Verification Code OTP
by Zuhaib Siddiqui
HTML
<div class="verification-code">
<label class="control-label">Verification Code</label>
<div class="verification-code--inputs">
<input type="text" maxlength="1" />
<input type="text" maxlength="1" />
<input type="text" maxlength="1" />
<input type="text" maxlength="1" />
<input type="text" maxlength="1" />
<input type="text" maxlength="1" />
</div>
<input type="hidden" id="verificationCode" name="verificationCode" value="" />
<span id="errorCodeMsg" style="display:none;"></span>
</div>
CSS
.verification-code {
max-width: 300px;
position: relative;
margin-right: 30px;
margin-bottom: 40px;
}
.verification-code ul {
list-style: none;
margin: 0;
padding: 0;
}
.verification-code ul > li {
display: inline-block;
}
.verification-code--inputs input[type=text] {
border: 2px solid #e1e1e1;
width: 46px;
height: 46px;
padding: 10px;
text-align: center;
display: inline-block;
font-size: 18px;
font-weight: bold;
}
.verification-code.success::after,
.verification-code.failed::after {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
display: inline-block;
font-style: normal;
font-variant: normal;
text-rendering: auto;
line-height: 1;
position: absolute;
top: 15px;
right: -30px;
}
.verification-code.success::after {
content: '\f00c';
color: #32b93b;
}
.verification-code.failed::after {
content: '\f00d';
color: #b93232;
}
JavaScript
//Code Verification
var verificationCode = [];
$(".verification-code input[type=text]").keyup(function (event) {
$(".verification-code input[type=text]").each(function (i) {
verificationCode[i] = $(".verification-code input[type=text]")[i].value;
$('#verificationCode').val(Number(verificationCode.join('')));
});
if ($(this).val().length > 0) {
if (event.key == 1 || event.key == 2 || event.key == 3 || event.key == 4 || event.key == 5 || event.key == 6 || event.key == 7 || event.key == 8 || event.key == 9 || event.key == 0) {
$(this).next().focus();
}
} else {
if (event.key == 'Backspace') {
$(this).prev().focus();
}
}
});