Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Input OTP for Svelte UI SDK

A one-time code field with one box per character, paste that fills every box, and digits-only or alphanumeric rules. For Svelte on UI SDK.

On this page9 sections

An input OTP collects a short code, such as the six digits of a sign-in code sent by text message, with one box per character. Typing moves on to the next box, and pasting the whole code fills them in one go.

Use it when the code has a fixed length. A secret of any length, such as a password, belongs in a Password Input, and free-form text in an Input.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Input OTP anatomy: root, group, separator, slot

Usage

<script lang="ts">
import {
  InputOTP, InputOTPGroup, InputOTPSlot,
} from "@/components/ui/input-otp";
</script>

Each InputOTPSlot takes its position in the code as index, counting from 0 across every group.

<InputOTP>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>

Accessibility

The slots are drawn over one real input that holds the whole code, so a screen reader hears a single text field, with autocomplete set to one-time-code and a number pad on phones for a digits-only code. Inside a Field, the FieldLabel names it by itself, as in the Form demo. Elsewhere, give InputOTP an aria-label, or a label whose for matches its id. Attributes on a slot only style it, since the slots are drawn div elements, hidden from screen readers.

Tab
Moves focus into the field and out of it again, since the whole code is one input.
Arrow Left
Moves to the previous slot.
Arrow Right
Moves to the next slot, as far as the first empty one.
Backspace
Deletes the character in the active slot, or the one before it when the active slot is empty. Characters after it move one slot left.
Delete
Deletes the character in the active slot. Characters after it move one slot left.
Home
Moves to the first slot.
End
Moves to the first empty slot, or the last slot once every slot is filled.
Ctrl+V
Pastes a code without its spaces and dashes, cut to the number of slots. A paste with a character the type or pattern doesn't take is refused.

Demos

Pattern

Six slots under the label Digits Only refuse letters, the right rule for a code sent by text message. type is set to numeric, the default when neither type nor pattern is set. When the issuer mixes letters and digits, allow both, as the Alphanumeric demo does.

Preview width

Separator

Six slots in three pairs, with a minus sign between the pairs. The separator isn't a character of the code, and the index on each slot keeps counting from 0 to 5 across the groups. Group the slots the way the code is printed in the message, such as 12-34-56, so people can check one against the other.

Preview width

Disabled

The code 123456 is filled in and disabled is set, so the slots fade and can't be focused or changed. Use it to show a code that has already been used, or a field that opens only after another step, such as choosing where to send the code. Don't disable the field to show an error, since people need to fix a wrong code.

Preview width

Controlled

The code lives in your own state, and the line under the slots repeats it, or asks for the code while it's empty. The value is one string. Control it when you check the code as it's typed, enable a Verify button once it's complete, or clear it after a failed attempt.

Preview width

Invalid

Every slot carries aria-invalid and the code starts as six zeros, so the red borders show before anyone types. Those marks only style the slots, which are drawn over one real input, so a screen reader hears no error. Set invalid on InputOTP as well to mark that input. Use it after the server rejects a code, next to a message that says so.

Preview width

Four digits

Four slots that take digits only, with no separator, for a four-digit PIN or a short code. Match the slot count to what the issuer sends, because two empty slots at the end make people think the code they got was cut short.

Preview width

Alphanumeric

Letters and digits both go in, split into two groups of three, for a code that mixes them, such as a backup code from an email. type is set to alphanumeric, which also switches a phone's keyboard to letters. A digits-only field refuses the first letter of such a code, which looks to people as if the field is broken.

Preview width

Form

A Verify your login card names the email the code went to, then shows the Verification code label, the slots with required, a Resend Code button and a full-width Verify button. The body and footer sit in a form, and InputOTP has a name, so Verify sends the whole code under it. Resend has type set to button, so pressing it doesn't submit.

Preview width

Auto submit

Six slots under One-Time Password check the code as soon as the last character goes in, with no button to press, and a line under the field then shows the code it took. The check runs from onValueComplete. Pick it for a sign-in code, where waiting for a click only slows people down.

Preview width

RTL

The label reads رمز التحقق, Arabic for verification code, and follows the field's right-to-left direction. The slots stay left to right, because the component lays them out that way in any direction and dir set to ltr on InputOTP keeps the caret and arrow keys in step. A code is read and pasted from its first character, so reversed slots would scramble it.

Preview width

FAQ

How do I control an Input OTP value in Svelte?

Bind the code with bind:value on InputOTP, as one string, or pass value and update it in onValueChange. That callback gets the new string first and details second, whose reason says whether it was typed, pasted or undone, and details.cancel() keeps the old code. The code is as long as its slots, the highest index plus one, unless you set maxLength.

The Controlled demo shows the code as it's typed.

How do I submit an Input OTP automatically when the code is complete?

Pass onValueComplete to InputOTP, which runs with the whole code once the last slot is filled, whether it was typed, pasted or filled in by the phone. Changing a character of a full code runs it again. From there, call requestSubmit on the form, or send the code to your server yourself, as the Auto submit demo does.

While the check runs, disable the field and show that it's working, so a second code can't be typed over the first.

How do I accept only digits in an Input OTP?

Digits only is already the default: type is numeric unless you set it to alphanumeric or alphabetic, and any other character typed or pasted is refused. For a rule of your own, pass pattern a regular expression, or a string of one, that each character must match, and it takes the place of type. Phones get a number pad only for numeric, so set inputMode to numeric on InputOTP for a pattern of digits.

The Pattern demo takes digits only.

How do I add a dash between Input OTP slots?

Put an InputOTPSeparator between two InputOTPGroup parts, as the Separator demo does. It draws a minus sign hidden from screen readers and isn't part of the code, so index keeps counting across groups: two groups of three are slots 0 to 2 and 3 to 5.

To draw something else, such as a dot, edit InputOTPSeparator in components/ui/input-otp, which is your file once installed.

How do I show an error on an Input OTP?

Set invalid on InputOTP after the server rejects a code, which puts aria-invalid on the real input a screen reader reads and turns every slot's border red. invalid on a Field around it does the same. Setting aria-invalid on each InputOTPSlot, as the Invalid demo does, only turns the borders red, since the slots are only drawn.

Say what went wrong in text next to the field, and clear the error once the person starts typing a new code.

How do I submit an Input OTP with a form?

Give InputOTP a name. The slots are drawn over one real input under that name, so the form submits the whole code as one string. Setting required blocks an empty or partly typed code, and without it the form submits whatever has been typed, so check the length on the server too.

The Form demo puts a verification card in a form and names the code. Its resend button has type set to button, so pressing it doesn't submit the code.

Can an Input OTP fill in a code from a text message or a paste?

Yes. The field sets autocomplete to one-time-code, the value browsers look for when they offer a code that just arrived by text message. Pasting a whole code fills the slots from the first, whichever one has focus, and a paste that breaks the digits-only or letters rule is refused.

One real input sits under the slots, so autofill goes into it like any text field. A pasted code loses its spaces and dashes first, so 123-456 fills six slots, and a code longer than the slots is cut to fit.

How do I use an Input OTP on a right-to-left page?

Keep the code left to right. The component already lays out its slots that way on any page, and setting dir to ltr on InputOTP, as the RTL demo does, makes the caret and the arrow keys agree with them. The label around it follows the page's direction as usual.

Don't reverse the indexes: slot 0 is the code's first character, on the left, wherever the field sits.

API Reference

REGEXP_ONLY_DIGITS

REGEXP_ONLY_CHARS

REGEXP_ONLY_DIGITS_AND_CHARS

InputOTP

<div> data-slot="otp-input"

PropType
value?string

The code, for a controlled input. Two-way with bind:value.

defaultValue?string | number | readonly string[]

The code it starts with, when uncontrolled.

Type
string | number | readonly string[]
onValueChange?(value: string, details: ChangeDetails<OTPInputValueChangeReason>) → void

Runs when the code changes: typing or deleting ("input-change"), a paste ("paste"), the browser's undo and redo, a form reset, or a controller. Autofill and codes from the keyboard's suggestions are "input-change". details.cancel() keeps the code as it was.

Type
(value: string, details: ChangeDetails<OTPInputValueChangeReason>) → void
Parameters

value – string

details – ChangeDetails<OTPInputValueChangeReason>

Returns
void
autoComplete?string

The input's autocomplete. "one-time-code" lets phones offer a code from a message, and password managers fill it.

Default
"one-time-code"
containerClassName?string

Classes for the container around the slots.

controller?{ getValue: () → string; setValue: (value: string, options?: OTPInputChangeOptions) → boolean; clear: (options?: OTPInputChangeOptions) → boolean; focus: () → void; }

Reads and writes the code from anywhere. Create one with createOTPInputController().

Type
{ getValue: () → string; setValue: (value: string, options?: OTPInputChangeOptions) → boolean; clear: (options?: OTPInputChangeOptions) → boolean; focus: () → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables it like a native input: it can't be focused, and isn't submitted.

form?string

The id of a form to belong to, when it isn't inside that form.

inputMode?"decimal" | "none" | "numeric" | "tel" | "text"

The keyboard touch screens show. UI SDK's default: numeric" for digits, "text" otherwise.

Type
"decimal" | "none" | "numeric" | "tel" | "text"
Default
(preset ? undefined : "numeric")
maxLength?number

How many characters the code has, one slot each.

Default
6
name?string

The name it submits the code under.

onChange?(value: string) → void

Runs with the new code whenever it changes.

Parameters

value – string

Returns
void
onComplete?(value: string) → void

Runs with the whole code once every slot is filled.

Parameters

value – string

Returns
void
onValueCommit?(value: string, details: CommitDetails<OTPInputValueChangeReason>) → void

Runs when a change fills every slot, with the whole code, like to verify it. A controller filling it counts, so does a code that changes while full.

Type
(value: string, details: CommitDetails<OTPInputValueChangeReason>) → void
Parameters

value – string

details – CommitDetails<OTPInputValueChangeReason>

Returns
void
pasteTransformer?(pasted: string) → string

Rewrites pasted text before it goes in, like dropping everything but digits.

Parameters

pasted – string

Returns
string
pattern?string | OTPInputPattern

Which codes it takes. A string is a regular expression the whole code must match, like REGEXP_ONLY_DIGITS; "digits", "letters", "alphanumeric" and a RegExp are checked per character. Without one, any character goes.

Type
string | OTPInputPattern
readOnly?boolean

Makes it read-only: focusable and submitted, but the code can't change.

required?boolean

Requires a code before the form submits. A partial code is too short either way.

textAlign?"center" | "end" | "start"

Where the invisible input keeps its text and caret. It decides where the browser puts its own popups, like the paste menu on phones. "start" and "end" follow the reading direction.

Type
"center" | "end" | "start"
Default
"start"
type?string

Ignored: the code lives in a text input the slots draw over.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-complete?string

Present while every slot is filled.

data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-filled?string

Present while the code holds any character.

data-focused?string

Present while the input has focus.

InputOTPGroup

<div> data-slot="otp-input-group"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-complete?string

Present while every slot is filled.

data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-filled?string

Present while the code holds any character.

data-focused?string

Present while the input has focus.

InputOTPSlot

<div> data-slot="otp-input-slot"

PropType
indexnumber

Which character of the code the slot shows, from 0.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-active?string

Present on the slot typing goes to: the selected character, or the empty slot after the last one. Only while focused.

data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-fake-caret?string

Present on the active slot while it's empty. Draw a blinking caret in it; the input's own caret is hidden.

data-filled?string

Present while the slot holds a character.

InputOTPSeparator

<div> data-slot="otp-input-separator"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.