Starting Point
v0.41.0

Search documentation

Search for a page and jump to it.

Start typing to search...
Go to Page
escClose

Tailwind CSS OTP Input

Use the input OTP component to enter one-time codes, like two-factor login, email verification, or a PIN. For regular text, use an input.

<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    maxlength="6"
    name="code"
    value="123456"
    inputmode="numeric"
    autocomplete="one-time-code"
    aria-label="One-time code"
  />
</label>

Usage

Add the input-otp class to a <label> with an empty <span> for each character, then an <input> whose maxlength matches the number of spans. Name the input with aria-label or a separate label.

ClassDescription
input-otpAdd to the <label> holding the boxes
input-otp-separatedAdd to input-otp to set the boxes apart
<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    name="code"
    maxlength="4"
    autocomplete="one-time-code"
    aria-label="One-time code"
  />
</label>

Examples

OTP input with a pattern

Add pattern to limit what can be typed and required to validate the code on submit. Add inputmode="numeric" for a number pad on phones.

<div class="field w-fit">
  <label class="label" for="otp-digits-only">Digits Only</label>
  <label class="input-otp">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <input
      maxlength="6"
      id="otp-digits-only"
      name="code"
      inputmode="numeric"
      pattern="[0-9]{6}"
      required
    />
  </label>
</div>

Separated OTP input

Add input-otp-separated to set the boxes apart.

<label class="input-otp input-otp-separated">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    maxlength="6"
    name="code"
    inputmode="numeric"
    aria-label="One-time code"
  />
</label>

Disabled

Add disabled to the input.

<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input maxlength="6" value="123456" aria-label="One-time code" disabled />
</label>

Controlled OTP input

Listen for input on the input to read the code as it is typed.

<div id="otp-controlled" class="grid justify-items-center gap-2">
  <label class="input-otp">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <input maxlength="6" inputmode="numeric" aria-label="One-time code" />
  </label>
  <div class="text-center text-sm" data-otp-message>
    Enter your one-time password.
  </div>
</div>
 
<script>
  const root = document.getElementById("otp-controlled");
  root.querySelector("input").addEventListener("input", (e) => {
    root.querySelector("[data-otp-message]").textContent = e.target.value
      ? `You entered: ${e.target.value}`
      : "Enter your one-time password.";
  });
</script>

Invalid

Add aria-invalid="true" to the input to show an error state.

<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    maxlength="6"
    value="000000"
    aria-label="One-time code"
    aria-invalid="true"
  />
</label>

Four digit PIN

Use four spans and a maxlength of 4 for a PIN.

<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    maxlength="4"
    name="pin"
    inputmode="numeric"
    pattern="[0-9]{4}"
    aria-label="PIN"
    required
  />
</label>

Alphanumeric OTP

Leave out inputmode, add spellcheck="false", and use a pattern like [a-zA-Z0-9]{6} to accept letters and numbers.

<label class="input-otp">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <span></span>
  <input
    maxlength="6"
    name="code"
    pattern="[a-zA-Z0-9]{6}"
    spellcheck="false"
    aria-label="One-time code"
    required
  />
</label>

OTP input in a form

The input submits the code as one field, named by its name.

<div class="card w-full max-w-md gap-0 py-0">
  <div class="card-content grid gap-6 px-6 py-8 sm:px-10">
    <div class="grid justify-items-center gap-2 text-center">
      <h1 class="text-xl font-medium tracking-tight">Authentication code</h1>
      <p class="text-sm text-balance text-muted-foreground">
        Enter the authentication code provided by your authenticator
        application.
      </p>
    </div>
    <form class="field-group" action="/two-factor-challenge" method="post">
      <div class="field items-center">
        <label class="label sr-only" for="otp-two-factor">
          Authentication code
        </label>
        <label class="input-otp input-otp-separated">
          <span></span>
          <span></span>
          <span></span>
          <span></span>
          <span></span>
          <span></span>
          <input
            id="otp-two-factor"
            name="code"
            maxlength="6"
            inputmode="numeric"
            autocomplete="one-time-code"
            pattern="[0-9]{6}"
            required
          />
        </label>
      </div>
      <button type="submit" class="btn">Continue</button>
    </form>
    <p class="text-center text-sm text-muted-foreground">
      or you can
      <a
        href="#"
        class="font-medium text-foreground underline-offset-4 hover:underline"
      >
        login using a recovery code
      </a>
    </p>
  </div>
</div>

RTL

Add dir="rtl" to any parent element, typically <html>, to render right-to-left. The code itself always reads left to right. See the RTL guide for details.

<div id="input-otp-rtl-demo" class="field w-fit" dir="rtl">
  <label class="label" for="input-otp-rtl" data-i18n="verificationCode">
    رمز التحقق
  </label>
  <label class="input-otp">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <input
      maxlength="6"
      id="input-otp-rtl"
      value="123456"
      inputmode="numeric"
    />
  </label>
</div>
 
<script>
  const translations = {
    ar: { dir: "rtl", verificationCode: "رمز التحقق" },
    he: { dir: "rtl", verificationCode: "קוד אימות" },
    en: { dir: "ltr", verificationCode: "Verification code" },
  };
  window.addEventListener("message", (e) => {
    const t = translations[e.data?.lang];
    if (e.data?.type !== "sp-language" || !t) return;
    document.getElementById("input-otp-rtl-demo").dir = t.dir;
    document.querySelectorAll("[data-i18n]").forEach((el) => {
      el.textContent = t[el.dataset.i18n];
    });
  });
</script>

CSS variables

Set these on the input-otp element, like [--input-otp-size:--spacing(12)] with text-xl for larger boxes.

VariableDefaultDescription
--input-otp-size2.25remWidth and height of a box
--input-otp-gap0 (0.5rem with input-otp-separated)Space between the boxes

JavaScript

Events

EventWhen
sp-completeEvery box is filled
document.querySelector(".input-otp").addEventListener("sp-complete", (e) => {
  console.log(e.detail.value);
});