About this snippet

Streamline two-factor authentication with this animated OTP sequence. The inputs auto-advance seamlessly as you type, gracefully handle pasting, and trigger a satisfying success glow upon completion.

Size
3.7 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
354
<div class="auth-card">
  <h2>Verification Code</h2>
  <p>We sent a 6-digit code to your device.</p>
  
  <div class="otp-container" id="otp-group">
    <input type="text" class="otp-box" maxlength="1" autofocus>
    <input type="text" class="otp-box" maxlength="1">
    <input type="text" class="otp-box" maxlength="1">
    <input type="text" class="otp-box" maxlength="1">
    <input type="text" class="otp-box" maxlength="1">
    <input type="text" class="otp-box" maxlength="1">
  </div>
  
  <button class="verify-btn">Verify Account</button>
</div>
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  margin: 0;
  background: #f8fafc;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  padding: 20px;
}

.auth-card {
  background: #ffffff;
  padding: 40px;
  border-radius: 24px;
  box-shadow: 0 20px 40px -10px rgba(0, 0, 0, 0.1);
  text-align: center;
  width: 100%;
  max-width: 450px;
  transition: box-shadow 0.4s ease;
}

.auth-card.success-state {
  box-shadow: 0 0 50px rgba(16, 185, 129, 0.4);
}

.auth-card h2 {
  color: #0f172a;
  margin: 0 0 10px 0;
  font-size: 1.75rem;
}

.auth-card p {
  color: #64748b;
  margin-bottom: 30px;
}

.otp-container {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 40px;
}

.otp-box {
  width: 50px;
  height: 60px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
  color: #0f172a;
  background: #f8fafc;
  transition: all 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  outline: none;
}

.otp-box:focus {
  border-color: #3b82f6;
  background: #ffffff;
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.15);
  transform: translateY(-2px);
}

.success-state .otp-box {
  border-color: #10b981;
  color: #10b981;
  background: #f0fdf4;
}

.verify-btn {
  width: 100%;
  background: #0f172a;
  color: #ffffff;
  border: none;
  padding: 16px;
  border-radius: 12px;
  font-size: 1.125rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.verify-btn:hover {
  background: #1e293b;
}

.success-state .verify-btn {
  background: #10b981;
}
const inputs = document.querySelectorAll(".otp-box");
const card = document.querySelector(".auth-card");

for (let i = 0; i < inputs.length; i++) {
  const currentInput = inputs.item(i);
  
  /* Auto advance when typing */
  currentInput.addEventListener("input", function(e) {
    if (currentInput.value !== "" && i < inputs.length - 1) {
      inputs.item(i + 1).focus();
    }
    checkSuccess();
  });
  
  /* Handle Backspace nicely */
  currentInput.addEventListener("keydown", function(e) {
    if (e.key === "Backspace" && currentInput.value === "" && i > 0) {
      inputs.item(i - 1).focus();
    }
  });
  
  /* Handle Pasting securely without regex or array brackets */
  currentInput.addEventListener("paste", function(e) {
    e.preventDefault();
    const pasteData = e.clipboardData.getData("text").trim();
    
    for (let j = 0; j < pasteData.length; j++) {
      if (i + j < inputs.length) {
        inputs.item(i + j).value = pasteData.charAt(j);
      }
    }
    
    /* Focus the last filled input safely */
    const targetIndex = Math.min(i + pasteData.length, inputs.length) - 1;
    inputs.item(targetIndex).focus();
    
    checkSuccess();
  });
}

function checkSuccess() {
  let isComplete = true;
  for (let i = 0; i < inputs.length; i++) {
    if (inputs.item(i).value === "") {
      isComplete = false;
    }
  }
  
  if (isComplete) {
    card.classList.add("success-state");
  } else {
    card.classList.remove("success-state");
  }
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save