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.
<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>Sent by SMS to the number ending 4321.
import { InputOTP } from "@/components/vin/input-otp";
export function InputOTPBasic() {
return <InputOTP label="Verification code" length={6} groups={[3, 3]} hint="Sent by SMS to the number ending 4321." />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import InputOTP from "@/components/vin/input-otp.vue";
</script>
<template>
<InputOTP label="Verification code" :length="6" :groups="[3, 3]" hint="Sent by SMS to the number ending 4321." />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import InputOTP from "$lib/components/vin/input-otp.svelte";
</script>
<InputOTP label="Verification code" length={6} groups={[3, 3]} hint="Sent by SMS to the number ending 4321." />Four-digit PIN
Any length, in one group.
<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>import { InputOTP } from "@/components/vin/input-otp";
export function InputOTPPin() {
return <InputOTP label="PIN" length={4} defaultValue="27" />;
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import InputOTP from "@/components/vin/input-otp.vue";
</script>
<template>
<InputOTP label="PIN" :length="4" default-value="27" />
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import InputOTP from "$lib/components/vin/input-otp.svelte";
</script>
<InputOTP label="PIN" length={4} value="27" />Error and disabled
An error outlines every box and is read out with the field.
That code has expired. Request a new one.
<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>That code has expired. Request a new one.
import { InputOTP } from "@/components/vin/input-otp";
export function InputOTPStates() {
return (
<div style={{ display: "grid", gap: 16 }}>
<InputOTP label="Code" length={6} groups={[3, 3]} defaultValue="482193" error="That code has expired. Request a new one." />
<InputOTP label="Backup PIN" length={4} defaultValue="1234" disabled />
</div>
);
}The Vue version renders the same markup as the HTML tab, so it looks and works the same.
<script setup lang="ts">
import InputOTP from "@/components/vin/input-otp.vue";
</script>
<template>
<div style="display: grid; gap: 16px">
<InputOTP label="Code" :length="6" :groups="[3, 3]" default-value="482193" error="That code has expired. Request a new one." />
<InputOTP label="Backup PIN" :length="4" default-value="1234" disabled />
</div>
</template>The Svelte version renders the same markup as the HTML tab, so it looks and works the same.
<script lang="ts">
import InputOTP from "$lib/components/vin/input-otp.svelte";
</script>
<div style="display: grid; gap: 16px">
<InputOTP label="Code" length={6} groups={[3, 3]} value="482193" error="That code has expired. Request a new one." />
<InputOTP label="Backup PIN" length={4} value="1234" disabled />
</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:
npx @vinish/ui add input-otpimport { InputOTP } from "@/components/vin/input-otp";import InputOTP from "@/components/vin/input-otp.vue";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.
| Prop | Values | Default | In HTML |
|---|---|---|---|
lengthNumber of characters. | number | 6 | maxlength + one .vin-otp__slot per character |
groupsSplit the boxes into groups, e.g. [3, 3]. | number[] | — | <span class="vin-otp__sep"> between groups |
value / defaultValueThe code typed so far. Use onValueChange; onComplete fires when every box is filled. | string | — | value attribute |
charset | numericalphanumeric | numeric | inputmode="numeric" or "text" |
label / hint / errorSame as Input's Field. | ReactNode | — | .vin-field, .vin-label, .vin-hint, .vin-error |
disabled | boolean | false | disabled attribute |
Parts
| React, Vue, Svelte | HTML 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.
Customize
Saved in this browser. Style and accent are in the header.