Skip to content

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

Components

Free

Input OTP for Svelte

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

On this page9 sections
UI library: Ark UI

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

Each slot is its own input, announced as "pin code 1 of 6" and so on, with autocomplete set to one-time-code so a phone can offer the code from a text message. Only one slot is in the tab order, and the keys below move between the others. Inside a Field, each slot reads the FieldLabel before its own name, and a click on the label puts focus in the first slot. To change the words "pin code", pass translations with an inputLabel function.

Arrow Left
Moves focus to the previous input.
Arrow Right
Moves focus to the next input, as far as the first empty one.
Backspace
Deletes the value in the current input and moves focus to the previous input.
Delete
Deletes the value in the current input.
Ctrl+V
Pastes the value into the input fields.
Enter
When every slot is filled and InputOTP has a name, submits the form it belongs to.
Home
Moves focus to the first slot.
End
Moves focus to the last filled slot, or to the first slot while every slot is empty.
Tab
Moves focus into the field at the first empty slot, or the last slot once every slot is filled, then out of the field.

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, which Ark UI also uses when you leave it out. 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 an array with one string per slot. 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. Each slot is a real input, so a screen reader hears the error on whichever one has focus. 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, and each slot sets its input mode to text so a phone shows 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?

Hold the code in state as an array with one string per slot, pass it as value and update it in onValueChange. Its details also carry valueAsString, the whole code as one string, which is what you send to the server.

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

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

Set autoSubmit on InputOTP, and Ark UI submits the surrounding form as soon as the last slot is filled. Give it a name too, so the code is in the form data. To run your own check instead, use onValueComplete, which receives the code as valueAsString, 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: Ark UI's 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 a regular expression string as pattern, which takes the place of type. Phones show a number pad while otp is on, as it is by default here, so set inputMode to text on each InputOTPSlot when the code has letters, as the Alphanumeric demo does.

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 with the separator role 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 every slot and turns their borders red. Setting aria-invalid on each InputOTPSlot, as the Invalid demo does, has the same effect.

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. Ark UI keeps a hidden input under that name holding the whole code as one string, so the form submits 123456 rather than six fields. Setting required on InputOTP makes that input required.

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 one that has focus, and a paste that breaks the digits-only or letters rule is refused.

On Ark UI each slot is a separate input with that attribute, and a code dropped into one slot is spread across the rest.

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

InputOTP

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

PropType
value?string[]

The controlled value of the the pin input.

defaultValue?string[]

The initial value of the the pin input when rendered. Use when you don't need to control the value of the pin input.

onValueChange?(details: ValueChangeDetails) → void

Function called on input change.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: string[]; valueAsString: string }

Returns
void
autoFocus?boolean

Whether to auto-focus the first input.

autoSubmit?boolean

Whether to auto-submit the owning form when all inputs are filled.

blurOnComplete?boolean

Whether to blur the input when the value is complete.

count?number

The number of inputs to render to improve SSR aria attributes. This will be required in next major version.

dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the inputs are disabled.

form?string

The associate form of the underlying input element.

id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the pin input. Useful for composition.

Type
{ root?: string; hiddenInput?: string; label?: string; control?: string; input?: (id: string) → string }
invalid?boolean

Whether the pin input is in the invalid state.

mask?boolean

If true, the input's value will be masked just like type=password.

name?string

The name of the input element. Useful for form submission.

onValueComplete?(details: ValueChangeDetails) → void

Function called when all inputs have valid values.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: string[]; valueAsString: string }

Returns
void
onValueInvalid?(details: ValueInvalidDetails) → void

Function called when an invalid value is entered.

Type
(details: ValueInvalidDetails) → void
Parameters

details – { value: string; index: number }

Returns
void
otp?boolean

If true, the pin input component signals to its fields that they should use autocomplete="one-time-code".

Default
true
pattern?string

The regular expression that the user-entered input value is checked against.

placeholder?string

The placeholder text for the input. Ark default: â—‹.

Default
""
readOnly?boolean

Whether the pin input is in the valid state.

required?boolean

Whether the pin input is required.

sanitizeValue?(value: string) → string

Function to sanitize pasted values before validation. Useful for stripping dashes, spaces, or other formatting.

Parameters

value – string

Returns
string
selectOnFocus?boolean

Whether to select input value when input is focused.

translations?object

Specifies the localized strings that identifies the accessibility elements and their states.

Type
{ inputLabel?: (index: number, length: number) → string }
type?"alphanumeric" | "numeric" | "alphabetic"

The type of value the pin-input should allow.

Type
"alphanumeric" | "numeric" | "alphabetic"
Default
"numeric"
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-complete?string

Present when the pin-input value is complete.

data-disabled?string

Present when disabled.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

InputOTPSlot

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

PropType
indexnumber

The slot's position in its list.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-complete?string

Present when the input value is complete.

data-disabled?string

Present when disabled.

data-filled?string

Present when filled.

data-indexstring

The index of the item.

data-invalid?string

Present when invalid.

data-ownedbystring

Id of the root part.

InputOtpControl

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

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>

InputOTPGroup

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

PropType
class?string

Merged with the part's classes.

InputOTPSeparator

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

PropType
class?string

Merged with the part's classes.