Input OTP
A one-time code field made of separate character slots, for verification codes and short PINs.
Type or paste a code. Focus moves across the slots on its own.
When to use
- To confirm a code sent by SMS, e-mail or an authenticator app.
- To collect a short numeric PIN with a fixed length.
When not to use
- Codes with a fixed 3 + 3 layout and no custom slots. Use Input OTP double, which comes laid out.
- Free text or variable-length values. Use Input.
- Passwords. Use Input with
type="password".
Anatomy
You compose the slots yourself, so the layout is yours to choose.
| Part | Role |
|---|---|
InputOTP |
The field. Holds the value, the length and the hidden native input. |
InputOTPGroup |
Groups neighbouring slots so they share borders. |
InputOTPSlot |
One character cell. index is its zero-based position. |
InputOTPSeparator |
A dot between groups. |
Examples
Six digits, two groups
Four digits, one group
4
8
Disabled
Content
- Label with what the code is and where it came from: “Código de verificação”.
- Say how many digits and where to find them in a description near the field.
- Offer a way to request a new code next to the field.
Accessibility
- Built on the
input-otplibrary: a single real<input>receives the keystrokes and the slots only draw them. Screen readers and password managers see one field. - Pass
autoComplete="one-time-code"so iOS and Android can offer the code from an SMS. - Pass
aria-labelor link a visibleLabel: the library does not name the field. pattern={REGEXP_ONLY_DIGITS}restricts input and opens the numeric keyboard.- The active slot gets a focus ring and a blinking caret. Slots are 40px wide.
- Paste fills all slots at once. Backspace and arrow keys work as in a text field.
Code
import { REGEXP_ONLY_DIGITS } from "input-otp";
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from "@repo/ui/input-otp";
<InputOTP
aria-label="Código de verificação de 6 dígitos"
autoComplete="one-time-code"
maxLength={6}
onChange={setCode}
pattern={REGEXP_ONLY_DIGITS}
value={code}
>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>;
Props
InputOTP| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Disables the whole field |
maxLength | number | — | Total number of slots. Required |
onChange | (value: string) => void | — | Called with the current code as the user types |
onComplete | (value: string) => void | — | Called once every slot is filled |
pattern | string | — | Regular expression every character must match. Use `REGEXP_ONLY_DIGITS` for numeric codes |
value | string | — | Controlled code |
InputOTPSlot| Prop | Type | Default | Description |
|---|---|---|---|
index | number | — | Zero-based position of the slot. Required. |
InputOTPGroup and InputOTPSeparator take no props of their own, only
className and native div attributes.
Known issues
| Issue | Effect |
|---|---|
| There is no error or success variant. | An invalid code can only be signaled outside the field, in text next to it. |
Disabled uses has-[:disabled]:opacity-50 on the container. |
A translucent treatment that reduces contrast. |