Here's a complete OTP input implementation with 6 fields:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OTP Input Test</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="otp-container">
<h2>Enter OTP Code</h2>
<div class="otp-inputs">
<input type="text" class="otp-field" id="otp-1" maxlength="1" autocomplete="off">
<input type="text" class="otp-field" id="otp-2" maxlength="1" autocomplete="off">
<input type="text" class="otp-field" id="otp-3" maxlength="1" autocomplete="off">
<input type="text" class="otp-field" id="otp-4" maxlength="1" autocomplete="off">
<input type="text" class="otp-field" id="otp-5" maxlength="1" autocomplete="off">
<input type="text" class="otp-field" id="otp-6" maxlength="1" autocomplete="off">
</div>
<button class="verify-btn" onclick="verifyOTP()">Verify OTP</button>
<div class="result" id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.otp-container {
background: white;
padding: 40px;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
text-align: center;
max-width: 400px;
width: 100%;
}
h2 {
color: #333;
margin-bottom: 30px;
font-size: 24px;
}
.otp-inputs {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 30px;
}
.otp-field {
width: 50px;
height: 50px;
border: 2px solid #ddd;
border-radius: 8px;
text-align: center;
font-size: 20px;
font-weight: bold;
color: #333;
background: #f9f9f9;
transition: all 0.3s ease;
outline: none;
}
.otp-field:focus {
border-color: #667eea;
background: white;
box-shadow: 0 0 10px rgba(102, 126, 234, 0.3);
transform: translateY(-2px);
}
.otp-field.filled {
background: #e8f4fd;
border-color: #667eea;
}
.verify-btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 30px;
border-radius: 25px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: all 0.3s ease;
margin-bottom: 20px;
}
.verify-btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.result {
font-size: 16px;
font-weight: bold;
min-height: 20px;
}
.result.success {
color: #27ae60;
}
.result.error {
color: #e74c3c;
}
@media (max-width: 480px) {
.otp-inputs {
gap: 5px;
}
.otp-field {
width: 40px;
height: 40px;
font-size: 18px;
}
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
const otpFields = document.querySelectorAll('.otp-field');
otpFields.forEach((field, index) => {
// Handle input event
field.addEventListener('input', function(e) {
const value = e.target.value;
// Only allow digits
if (!/^\d$/.test(value)) {
e.target.value = '';
return;
}
// Add filled class for styling
e.target.classList.add('filled');
// Move to next field
if (value && index < otpFields.length - 1) {
otpFields[index + 1].focus();
}
});
// Handle keydown for backspace
field.addEventListener('keydown', function(e) {
if (e.key === 'Backspace') {
if (!e.target.value && index > 0) {
// Move to previous field if current is empty
otpFields[index - 1].focus();
otpFields[index - 1].value = '';
otpFields[index - 1].classList.remove('filled');
} else if (e.target.value) {
// Clear current field
e.target.classList.remove('filled');
}
}
// Prevent non-numeric input
if (!/[\d\b]/.test(e.key) && !['ArrowLeft', 'ArrowRight', 'Delete', 'Tab'].includes(e.key)) {
e.preventDefault();
}
});
// Handle paste event
field.addEventListener('paste', function(e) {
e.preventDefault();
const pastedData = e.clipboardData.getData('text');
const digits = pastedData.replace(/\D/g, '').slice(0, 6);
digits.split('').forEach((digit, i) => {
if (i < otpFields.length) {
otpFields[i].value = digit;
otpFields[i].classList.add('filled');
}
});
// Focus on the next empty field or last field
const nextEmpty = digits.length < otpFields.length ? digits.length : otpFields.length - 1;
otpFields[nextEmpty].focus();
});
// Handle focus - select all text
field.addEventListener('focus', function(e) {
e.target.select();
});
});
});
// Function to verify OTP (for demonstration)
function verifyOTP() {
const otpFields = document.querySelectorAll('.otp-field');
const resultDiv = document.getElementById('result');
let otp = '';
otpFields.forEach(field => {
otp += field.value;
});
if (otp.length !== 6) {
resultDiv.textContent = 'Please enter all 6 digits';
resultDiv.className = 'result error';
return;
}
// Demo verification - in real app, this would be sent to server
resultDiv.textContent = `OTP Entered: ${otp}`;
resultDiv.className = 'result success';
console.log('OTP Value:', otp);
}
Final Result
The code creates a professional OTP input interface with these features:
Functionality:
- Only accepts single digits (0-9) in each field
- Automatically moves focus to the next field after entering a digit
- Backspace moves to the previous field when current field is empty
- Supports pasting a 6-digit code that fills all fields automatically
- Visual feedback with color changes and animations
User Experience:
- Responsive design that works on mobile devices
- Smooth transitions and hover effects
- Clear visual feedback for focused and filled fields
- Professional gradient background and styling
Testing the Implementation:
- Type digits one by one - focus automatically moves forward
- Use backspace to go back and correct digits
- Copy/paste a 6-digit code like "123456" to test paste functionality
- Click "Verify OTP" to see the entered code
- Try typing non-numeric characters (they'll be rejected)
The OTP input is ready for integration into any authentication system and provides an excellent user experience with smooth keyboard navigation and visual feedback.