webdevpuneet.com

Bootstrap Two-Factor Verification Code Form — Free Snippet

Bootstrap Two-Factor Verification Code Form · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

Copy & paste

Source Code

<div class="container py-5 d-flex justify-content-center">
  <div class="card bs2fa-card">
    <div class="card-body p-4 text-center">
      <h5 class="fw-bold mb-1">Enter verification code</h5>
      <p class="text-muted small mb-4">We sent a 6-digit code to your email.</p>

      <div class="d-flex gap-2 justify-content-center mb-3" id="bs2faDigits">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
        <input type="text" inputmode="numeric" maxlength="1" class="form-control text-center bs2fa-box">
      </div>

      <p class="small mb-3" id="bs2faStatus">&nbsp;</p>
      <button class="btn btn-dark w-100 fw-bold" id="bs2faVerify" disabled>Verify</button>
      <p class="small text-muted mt-3 mb-0">Didn't get it? <a href="javascript:void(0)" id="bs2faResend">Resend code</a></p>
    </div>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Real Bootstrap 5.3 form-control inputs styled as a six-box code entry
Auto-advances focus to the next box as each digit is typed
Backspace on an empty box moves focus back to the previous box
Pasting a full code distributes it across all six boxes correctly, not just the clicked one
Verify button stays disabled until all six boxes are filled
Non-digit characters are stripped automatically as you type

About this UI Snippet

Bootstrap Two-Factor Verification Code Form — HTML, CSS & JavaScript

Screenshot of the Bootstrap Two-Factor Verification Code Form snippet rendered live

A six-box verification code input needs to feel like typing one number, not managing six separate fields — this snippet implements the three behaviors that make that true, on top of real Bootstrap 5.3 form-control inputs. Typing a digit auto-advances focus to the next box; pressing Backspace on an empty box moves focus back to the previous one, so deleting feels continuous in both directions; and pasting a full code into any box — the way a password manager or a copied SMS code actually arrives — distributes all six digits across the boxes at once via the paste event, rather than dumping the whole string into a single maxlength="1" field.

The Verify button stays disabled until all six boxes are filled, and comparing against a fixed CORRECT code demonstrates both the success and failure paths, including marking every box is-invalid on a wrong code.

Step by step

How to Use

  1. 1
    Load the snippetClick the snippet in the sidebar Library tab. The preview loads six empty digit boxes.
  2. 2
    Type digits one at a timeFocus auto-advances to the next box after each digit — no clicking required between boxes.
  3. 3
    Press Backspace on an empty boxFocus moves back to the previous box, letting you delete continuously across boxes.
  4. 4
    Paste "482913"Copy that number and paste it into any box — all six digits distribute correctly across the six boxes at once.
  5. 5
    Click VerifyA correct code shows a green success message; any other 6 digits show a red error and mark every box invalid.

Real-world uses

Common Use Cases

Two-factor authentication and OTP verification
The standard six-box code entry pattern used by virtually every 2FA and one-time-password flow.
Learning multi-input focus management
A clear example of auto-advance, backspace-to-previous, and paste distribution — the three behaviors that make a segmented input feel unified.
Reducing friction in a security-critical flow
Paste support in particular matters a lot here — many users copy a code from an SMS or authenticator app rather than typing it.
Any fixed-length code entry, not just 2FA
Reuse the same pattern for invite codes, coupon codes, or any other short fixed-length input.

Got questions?

Frequently Asked Questions

Yes — a paste event listener intercepts the pasted text, strips non-digit characters, and distributes up to six digits across the six boxes in order, rather than the browser's default behavior of dropping the whole string into whichever single box was focused.

The distribution always starts from the first box (index 0), regardless of which box you pasted into — this matches how most real 2FA inputs behave, since a full code paste is meant to fill the whole sequence.

If a box is empty and Backspace is pressed, a keydown listener moves focus to the previous box — pressing Backspace again there deletes its digit, so repeated backspacing clears the code right-to-left continuously.

Yes, for this front-end demo (482913) — replace the comparison in the Verify handler with a real API call validating the code server-side.

No — each input event strips anything that isn't a digit, so only 0–9 ever ends up in a box regardless of what's typed or pasted.