Skip to content
loseta

Components / Forms

Input OTP

A single native input for one-time codes.

Status stableWorks with button, card, field, label
<input class="input-otp" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" aria-label="One-time code" placeholder="000000">

Installation

pnpm dlx loseta add input-otp
npx loseta add input-otp

Examples

Sizes

<div style="display: flex; flex-direction: column; gap: 1rem; max-width: 24rem">
  <div class="field">
    <label class="label" for="demo-input-otp-sm">Small</label>
    <input class="input-otp" data-size="sm" id="demo-input-otp-sm" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" placeholder="000000">
  </div>
  <div class="field">
    <label class="label" for="demo-input-otp-default">Default</label>
    <input class="input-otp" id="demo-input-otp-default" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" placeholder="000000">
  </div>
  <div class="field">
    <label class="label" for="demo-input-otp-lg">Large</label>
    <input class="input-otp" data-size="lg" id="demo-input-otp-lg" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" placeholder="000000">
  </div>
</div>

Four digits

Enter the 4-digit code we sent to your email.

<div class="field" style="max-width: 20rem">
  <label class="label" for="demo-input-otp-four-digits">Four-digit code</label>
  <input class="input-otp" id="demo-input-otp-four-digits" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="4" pattern="[0-9]*" placeholder="0000" aria-describedby="demo-input-otp-four-digits-hint">
  <p id="demo-input-otp-four-digits-hint">Enter the 4-digit code we sent to your email.</p>
</div>

Alphanumeric

Use letters and numbers without spaces.

<div class="field" style="max-width: 20rem">
  <label class="label" for="demo-input-otp-alphanumeric">Backup code</label>
  <input class="input-otp" id="demo-input-otp-alphanumeric" type="text" autocomplete="one-time-code" autocapitalize="characters" spellcheck="false" maxlength="6" pattern="[A-Za-z0-9]*" placeholder="ABC123" aria-describedby="demo-input-otp-alphanumeric-hint">
  <p id="demo-input-otp-alphanumeric-hint">Use letters and numbers without spaces.</p>
</div>

Disabled

Code entry is unavailable right now.

<div class="field" style="max-width: 24rem">
  <label class="label" for="demo-input-otp-disabled">One-time code</label>
  <input class="input-otp" id="demo-input-otp-disabled" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" value="123456" disabled>
  <p>Code entry is unavailable right now.</p>
</div>

Invalid

<div class="field" style="max-width: 24rem">
  <label class="label" for="demo-input-otp-invalid">One-time code</label>
  <input class="input-otp" id="demo-input-otp-invalid" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" value="000000" aria-invalid="true" aria-describedby="demo-input-otp-invalid-error">
  <p id="demo-input-otp-invalid-error" role="alert">That code is incorrect. Check your email and try again.</p>
</div>

Verification

Verify your login

Enter the verification code we sent to ada@example.com.

The code expires 10 minutes after it is sent.

<div class="card" style="max-width: 24rem">
  <header>
    <h3 data-slot="title">Verify your login</h3>
    <p data-slot="description">Enter the verification code we sent to <strong style="font-weight: 500; color: var(--foreground)">ada@example.com</strong>.</p>
  </header>
  <section>
    <div class="field">
      <div style="display: flex; align-items: center; justify-content: space-between; gap: 1rem">
        <label class="label" for="demo-input-otp-verification">Verification code</label>
        <button type="button" class="btn" data-variant="outline" data-size="xs"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" data-icon="inline-start"><path d="M21 12a9 9 0 0 0-9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/><path d="M3 12a9 9 0 0 0 9 9 9.75 9.75 0 0 0 6.74-2.74L21 16"/><path d="M16 16h5v5"/></svg>Resend code</button>
      </div>
      <input class="input-otp" id="demo-input-otp-verification" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" required placeholder="000000" aria-describedby="demo-input-otp-verification-hint">
      <p id="demo-input-otp-verification-hint">The code expires 10 minutes after it is sent.</p>
    </div>
  </section>
  <footer>
    <div class="field">
      <button type="button" class="btn" style="width: 100%">Verify</button>
      <p style="text-align: center; color: var(--muted-foreground)">Having trouble signing in? <a href="#">Contact support</a></p>
    </div>
  </footer>
</div>

Right-to-left

أدخل الرمز المكوّن من ستة أرقام الذي أرسلناه إلى بريدك الإلكتروني.

<div class="field" dir="rtl" lang="ar" style="max-width: 24rem">
  <label class="label" for="demo-input-otp-rtl">رمز التحقق</label>
  <input class="input-otp" id="demo-input-otp-rtl" dir="rtl" type="text" inputmode="numeric" autocomplete="one-time-code" maxlength="6" pattern="[0-9]*" value="123456" aria-describedby="demo-input-otp-rtl-hint">
  <p id="demo-input-otp-rtl-hint">أدخل الرمز المكوّن من ستة أرقام الذي أرسلناه إلى بريدك الإلكتروني.</p>
</div>

Accessibility and behavior

  • A single native input, centered with tabular numerals and compensated letter spacing. Use inputmode="numeric", autocomplete="one-time-code", maxlength and pattern; data-size="sm|lg" changes the height and type scale, and aria-invalid plus disabled follow the field states. CSS draws one field, not per-digit slots, and does not auto-advance; those need consumer behavior. Size it with your layout.

Files

components/input-otp.cssbase
styles/nova/input-otp.cssstyle

Install contract

The CLI writes the base file under components/, the visual file for the selected style under styles/<name>/, and any primitive dependency underprimitives/. The generated index.css imports them in the right layer order.

← Back to components

loseta is MIT licensed. Built with native CSS, no runtime. Inspired byshadcn/ui.