Skip to content

Input OTP

Enter a one-time code, one box per character. Same markup and props in every style: switch the style in the header to see it change.

Basic

Six digits in two groups. Paste a code or let the phone fill it from an SMS. In HTML, add data-vin-otp and include vinui.js.

Sent by SMS to the number ending 4321.

input-otp-basic.html
<div class="vin-field">
  <label class="vin-label" for="otp">Verification code</label>
  <div class="vin-otp" data-vin-otp>
    <input id="otp" class="vin-otp__input" type="text" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]*" maxlength="6" aria-describedby="otp-hint">
    <div class="vin-otp__slots" aria-hidden="true">
      <span class="vin-input vin-otp__slot"></span><span class="vin-input vin-otp__slot"></span><span class="vin-input vin-otp__slot"></span>
      <span class="vin-otp__sep"></span>
      <span class="vin-input vin-otp__slot"></span><span class="vin-input vin-otp__slot"></span><span class="vin-input vin-otp__slot"></span>
    </div>
  </div>
  <p class="vin-hint" id="otp-hint">Sent by SMS to the number ending 4321.</p>
</div>

Four-digit PIN

Any length, in one group.

input-otp-pin.html
<div class="vin-field">
  <label class="vin-label" for="pin">PIN</label>
  <div class="vin-otp" data-vin-otp>
    <input id="pin" class="vin-otp__input" type="text" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]*" maxlength="4" value="27">
    <div class="vin-otp__slots" aria-hidden="true">
      <span class="vin-input vin-otp__slot">2</span><span class="vin-input vin-otp__slot">7</span><span class="vin-input vin-otp__slot"></span><span class="vin-input vin-otp__slot"></span>
    </div>
  </div>
</div>

Error and disabled

An error outlines every box and is read out with the field.

That code has expired. Request a new one.

input-otp-states.html
<div style="display: grid; gap: 16px">
  <div class="vin-field">
    <label class="vin-label" for="otp-err">Code</label>
    <div class="vin-otp" data-vin-otp>
      <input id="otp-err" class="vin-otp__input" type="text" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]*" maxlength="6" value="482193" aria-invalid="true" aria-describedby="otp-err-error">
      <div class="vin-otp__slots" aria-hidden="true">
        <span class="vin-input vin-otp__slot">4</span><span class="vin-input vin-otp__slot">8</span><span class="vin-input vin-otp__slot">2</span>
        <span class="vin-otp__sep"></span>
        <span class="vin-input vin-otp__slot">1</span><span class="vin-input vin-otp__slot">9</span><span class="vin-input vin-otp__slot">3</span>
      </div>
    </div>
    <p class="vin-error" id="otp-err-error">That code has expired. Request a new one.</p>
  </div>
  <div class="vin-field">
    <label class="vin-label" for="otp-off">Backup PIN</label>
    <div class="vin-otp" data-vin-otp>
      <input id="otp-off" class="vin-otp__input" type="text" inputmode="numeric" autocomplete="one-time-code" pattern="[0-9]*" maxlength="4" value="1234" disabled>
      <div class="vin-otp__slots" aria-hidden="true">
        <span class="vin-input vin-otp__slot">1</span><span class="vin-input vin-otp__slot">2</span><span class="vin-input vin-otp__slot">3</span><span class="vin-input vin-otp__slot">4</span>
      </div>
    </div>
  </div>
</div>

Installation

React, Vue and Svelte: add the component to your project with the CLI. It reads your framework from vin.json and also copies any component this one needs. Then import it:

Terminal
npx @vinish/ui add input-otp
React
import { InputOTP } from "@/components/vin/input-otp";
Vue
import InputOTP from "@/components/vin/input-otp.vue";
Svelte
import InputOTP from "$lib/components/vin/input-otp.svelte";

The import paths assume the default folders: @/components/vin (React and Vue, with the usual @ → src alias) and $lib/components/vin (SvelteKit). Use your own if you chose another folder in init. How the Vue and Svelte versions pass values and content is in Frameworks.

Plain HTML: the classes come with the style's stylesheet. This component also needs vinui.js for its keyboard and pointer behaviour.

API

From spec/input-otp.json, which every framework version is tested against.

PropValuesDefaultIn HTML
length
Number of characters.
number6maxlength + one .vin-otp__slot per character
groups
Split the boxes into groups, e.g. [3, 3].
number[]—<span class="vin-otp__sep"> between groups
value / defaultValue
The code typed so far. Use onValueChange; onComplete fires when every box is filled.
string—value attribute
charsetnumericalphanumericnumericinputmode="numeric" or "text"
label / hint / error
Same as Input's Field.
ReactNode—.vin-field, .vin-label, .vin-hint, .vin-error
disabledbooleanfalsedisabled attribute

Parts

React, Vue, SvelteHTML classes
<InputOTP>.vin-otp.vin-otp__input.vin-otp__slots.vin-otp__slot.vin-otp__sep

Accessibility

  • One real text input (not six), so screen readers, paste, password managers and SMS autofill (autocomplete="one-time-code") all work.
  • The boxes are drawn for sight only and hidden from screen readers; the input carries the value.
  • inputmode="numeric" brings up the number pad on phones.
  • The input has a label; errors set aria-invalid and are linked with aria-describedby.