OTP Input UI
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OTP Input UI</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* Centering the container and styling the inputs */
.otp-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}
.otp-input {
width: 50px;
height: 50px;
font-size: 24px;
text-align: center;
border: 2px solid #ced4da;
border-radius: 5px;
margin: 0 8px;
outline: none;
transition: border-color 0.3s ease-in-out;
}
.otp-input:focus {
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
.container {
max-width: 500px;
margin: auto;
}
.btn-primary {
font-size: 18px;
padding: 10px 20px;
}
</style>
</head>
<body>
<div class="container mt-5">
<h2 class="text-center">Enter OTP</h2>
<p class="text-center text-muted">Enter the 6-digit code sent to your registered number</p>
<div class="otp-container">
<input type="text" maxlength="1" class="form-control otp-input" data-index="0" />
<input type="text" maxlength="1" class="form-control otp-input" data-index="1" />
<input type="text" maxlength="1" class="form-control otp-input" data-index="2" />
<input type="text" maxlength="1" class="form-control otp-input" data-index="3" />
<input type="text" maxlength="1" class="form-control otp-input" data-index="4" />
<input type="text" maxlength="1" class="form-control otp-input" data-index="5" />
</div>
<div class="text-center mt-4">
<button class="btn btn-primary" id="submitOtp">Submit OTP</button>
</div>
</div>
<!-- Bootstrap JS and jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function () {
// Move to the next input and select its value
$(".otp-input").on("input", function () {
const input = $(this);
const index = parseInt(input.data("index"));
const value = input.val();
// Automatically focus and select the next input field
if (value.length === 1) {
const nextInput = $(`.otp-input[data-index='${index + 1}']`);
if (nextInput.length) {
nextInput.focus().select();
}
}
});
// Handle backspace to move to the previous input and select its value
$(".otp-input").on("keydown", function (e) {
const input = $(this);
const index = parseInt(input.data("index"));
if (e.key === "Backspace" && input.val() === "") {
const prevInput = $(`.otp-input[data-index='${index - 1}']`);
if (prevInput.length) {
prevInput.focus().select();
}
}
});
// Gather OTP and submit
$("#submitOtp").on("click", function () {
const otp = $(".otp-input")
.map(function () {
return $(this).val();
})
.get()
.join("");
alert(`Entered OTP: ${otp}`);
});
});
</script>
</body>
</html>