Skip to content

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

Components

Free

Input OTP for UI SDK

A one-time code field with one box per character, paste that fills every box, and digits-only or alphanumeric rules. For React 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

import {
  InputOTP, InputOTPGroup, InputOTPSlot,
} from "@/components/ui/input-otp";

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. Inside a Field, the FieldLabel names it by itself, as in the Form demo. Elsewhere, give InputOTP an aria-label, or a label whose htmlFor 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, cut to the number of slots. With a pattern, a paste that doesn't match it is refused.

Demos

Pattern

Six slots under the label Digits Only refuse letters, the right rule for a code sent by text message. pattern is set to REGEXP_ONLY_DIGITS, which the component's own file exports, and without it any character goes in. 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. The slots are drawn over one real input, so the demo sets aria-invalid on InputOTP too, which passes it to that input for a screen reader. 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. pattern is set to REGEXP_ONLY_DIGITS_AND_CHARS, and inputMode 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 onComplete. 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 React?

Hold the code in state as one string, pass it as value and update it in onChange, which receives the new string. onValueChange gets the same string with details, whose reason says whether it was typed, pasted or undone, and details.cancel() keeps the old code. Set maxLength to the number of slots, 6 when left out, and give the slots the indexes 0 up to one less than that.

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

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

Pass onComplete to InputOTP, and it runs with the code once the last slot is filled, whether the code 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?

Pass pattern to InputOTP. The component's own file exports REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS_AND_CHARS, which check each character typed or pasted, while a regular expression string of your own must match the whole code. Without a pattern any character goes in, and phones show a number pad except with the two exported patterns that allow letters, so set inputMode to text for a pattern of your own that has them.

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 aria-invalid on each InputOTPSlot, which turns its border red, and on InputOTP, which passes it to the real input a screen reader reads, since the slots themselves are only drawn. The Invalid demo sets both.

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.

On this base one real input sits under the slots, so autofill goes into it like any text field. With a pattern, a pasted code loses its spaces and dashes first, so 123-456 fills six slots. Without one, the paste goes in as the input-otp library puts it, spaces and dashes included, cut to the slots left.

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.

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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?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?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

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