Input OTP

A single native input presented as a group of one-time-code slots.

Usage

Load the styles and scripts

Load a full Basecoat style bundle, or import base/base.css, components/input.css, components/input-otp.css, and one style pack after Tailwind. Include the Basecoat runtime and input-otp.js, or use all.js.

Add the input and slots

Keep one real text input with a label, name, and maxlength. The visual slots are hidden from assistive technology. Their count must match maxlength; group them in <div> elements and use an optional decorative SVG between groups.

<div class="input-otp" id="otp-example">
  <input class="input" type="text" name="code" aria-label="Verification code" inputmode="numeric" autocomplete="one-time-code" spellcheck="false" maxlength="6" value="" dir="ltr" />
  <div aria-hidden="true">
    <div>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
    </div>
  </div>
</div>

Without JavaScript, the ordinary input remains visible and usable. With JavaScript, it remains the single keyboard target and submitted form value. Typing replaces the selected character and advances to the next slot. To replace a filled code with a pasted one, select the whole field first.

inputmode="numeric" requests a numeric keyboard. Add data-filter to enforce the input’s pattern while typing and pasting. Choose a pattern that accepts partial codes, such as [0-9]+, and use minlength if the form requires a complete code. Use type="text" to preserve leading zeroes. Actual SMS autofill depends on the browser, device, and message format.

JavaScript API

Native input, change, and form reset events remain available. After setting .value from your application, call document.getElementById("otp-example").refresh() to update the slots. No separate value store or custom completion event is needed.

Examples

Pattern

This example accepts digits and rejects a paste containing letters.

Separator

Disabled

Current value

Read the current code from the native input event.

Enter your one-time password.

Invalid

Put aria-invalid="true" on the real input and associate the error using aria-describedby.

The code is incorrect. Please try again.

Four digits

Alphanumeric

Allow letters and digits when typing or pasting.

Form

Verify uses native form validation. Resending a code and recovering an account require application endpoints. The example reports these actions locally.

Verify your login

Enter the verification code we sent to your email address: m@example.com.

I no longer have access to this email address.

RTL

The form and slots follow the page direction.