Skip to content

OTP Code Input Boxes

Six-digit Tailwind CSS OTP verification input with auto-advance, paste support, backspace navigation, resend timer and simulated check.

Also known as: otp input boxes, one time password input, six digit verification field, sms passcode entry

Tailwind CSS Inputs Responsive #form #input #otp #responsive #verification

OTP Code Input Boxes live demo

Open in new tab

Tailwind CSS code (HTML, CSS, JS)

A one-time password (OTP) input built with Tailwind CSS and vanilla JavaScript: six separate digit boxes that auto-advance as you type, accept a pasted code, and verify it with a simulated request. Use it for email or SMS verification, two-factor sign-in and checkout confirmation screens.

How it works

The root is .tox-otp, a full-height gradient backdrop holding a white card. The form (data-tox-form) contains a fieldset with a visually hidden legend and six input.tox-digit fields inside data-tox-group. Each box is a text input with inputmode="numeric" and pattern="[0-9]*", so phones open the number pad. The first one has autocomplete="one-time-code", letting iOS and Android offer the code from an SMS. A hidden data-tox-value input always holds the combined six digits for a normal form post.

The script initializes every .tox-otp on DOMContentLoaded. On input, a single keystroke replaces the current digit and moves focus to the next box; longer values from autofill are spread across the boxes by fill(). On keydown, Backspace on an empty box clears the previous one and steps back, Arrow Left/Right, Home and End move between boxes, and letters are blocked. The paste handler strips non-digits, so "246 810" works, and a full code always starts in the first box. When all six boxes are filled the form submits itself. The submit handler shows a spinner, waits 900 ms, then compares the code with data-tox-expected and sets data-state="success" or data-state="error" on the root. The "Resend code" button starts a countdown from data-tox-resend seconds.

Customization

  • data-tox-expected on .tox-otp: the demo code that verifies; in production remove it and replace the setTimeout in the submit handler with a fetch() to your verification endpoint.
  • data-tox-resend on .tox-otp: seconds before "Resend code" becomes clickable again, e.g. 60.
  • Number of boxes: add or remove input.tox-digit elements and update their aria-label; the script reads the count from the markup.
  • .tox-digit[data-filled] in style.css: change border-color to restyle boxes that already hold a digit.
  • .tox-otp[data-state="error"] and [data-state="success"] rules: set your own error and success colors; tox-shake controls the error wobble.
  • from-violet-600 via-indigo-600 to-indigo-800 on the root and bg-violet-600 on the submit button: swap for another Tailwind palette to rebrand.
  • Remove the span separator between the third and fourth box for an evenly spaced row.

Use cases

  • Email or SMS verification: six digit boxes auto-advance as the user types and accept a pasted code
  • Two-factor sign-in: the first box offers the one-time code from an SMS on phones
  • Checkout confirmation: the form submits itself once all digits are filled and shows success or error
  • Account security screens: a resend button counts down before the user can request another code

Accessibility

Each box has an aria-label such as "Digit 3 of 6", and the fieldset links the instructions and the result through aria-describedby. The message paragraph is an aria-live="polite" region that announces checking, errors and success. Wrong codes set aria-invalid="true" on every box and return focus to the first one. Boxes show a 4px violet focus ring, buttons use focus-visible outlines, and the submit button stays disabled until the code is complete. The shake animation and spinner stop under prefers-reduced-motion: reduce.

Usage notes

Several instances can share a page because every query is scoped to its own .tox-otp root; give each copy unique id values for tox-desc and tox-message.

OTP Code Input Boxes at a glance

Framework
Tailwind CSS
Code
HTML, CSS, JavaScript
JavaScript
Vanilla JavaScript, no jQuery
Dependencies
Tailwind CSS
Layout
Responsive, works on mobile
Category
Inputs
License
Free for personal and commercial use

Questions about this template

Can I use the OTP Code Input Boxes code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

Load Tailwind CSS from a CDN (the links are under Requirements). Paste the HTML where the component should appear, add the CSS to your stylesheet and load the JavaScript before the closing </body> tag. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. The script is plain vanilla JavaScript, so it works without jQuery, React or Vue. You can still use the markup inside those frameworks.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

Add these to your page (already included in the download):

  • <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

The Tailwind browser build is for prototyping. In production, compile Tailwind with its CLI or your bundler.

OTP Code Input Boxes – Tailwind CSS inputs component screenshot
OTP Code Input Boxes · Tailwind CSS Inputs

More Inputs

Password Strength Meter – Bootstrap 5 inputs preview

Password Strength Meter

Bootstrap 5 password input with show/hide toggle, four-step strength meter, live requirements checklist and confirmation match check.

Inputs with Icons and Add-ons – Tailwind CSS inputs preview

Inputs with Icons and Add-ons

Tailwind CSS input group examples with leading icons, text prefixes and suffixes, a currency select, password toggle, clear button and helper text.

Tag Input with Chips – Bootstrap 6 inputs preview

Tag Input with Chips

Dark Bootstrap 6 tag input that turns typed values into removable Chips, with suggestions, a tag limit, keyboard shortcuts and a live post preview.

Form Validation Styles – Bootstrap 6 inputs preview

Form Validation Styles

Bootstrap 6 signup form showing valid and invalid field states with specific feedback messages, a live status checklist and a simulated submit.

OTP Code Input – Bootstrap 6 inputs preview

OTP Code Input

Dark Bootstrap 6 OtpInput verification card with 6 digit slots, paste support, auto-advance, auto-verify on complete, error state and a resend countdown.

Password Input with Strength Meter – Bootstrap 6 inputs preview

Password Input with Strength Meter

Bootstrap 6 password field with show/hide toggle, the Strength meter, a live requirement checklist, confirm match, Caps Lock hint and a password generator.