Enter the 6-digit code we sent to tony@example.com.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputBasic() { return ( <Field className="w-fit"> <FieldLabel>Verification code</FieldLabel> <OTPInput length={6} /> <FieldDescription>Enter the 6-digit code we sent to tony@example.com.</FieldDescription> </Field> );}- Completed:Add the registryOnce per project
pnpm dlx shadcn@latest registry add @jarvis=https://ui.jarv.is/r/{name}.json - Completed:Add otp-input
pnpm dlx shadcn@latest add @jarvis/otp-input
Usage
import { Field, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export function CodeField() { return ( <Field> <FieldLabel>Code</FieldLabel> <OTPInput length={4} /> </Field> );}OTPInput renders a box for each of its length characters. In a Field, the
FieldLabel labels the first box, and screen readers announce each of the others by its position,
such as "Character 2 of 4". To change that wording, lay out the boxes yourself and give each
OTPInputSlot an aria-label.
To lay out the boxes yourself, pass OTPInput the parts as children, as in
Composing the boxes.
Composition
Examples
Checking the code
onValueComplete is called once every box is filled. When the code is wrong, set invalid on the
Field to mark every box invalid, and show a FieldError.
Try 424242, or any other code to see an error.
import * as React from "react";
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";import { toast } from "@/components/ui/toast";
export default function OTPInputVerify() { const [code, setCode] = React.useState(""); const [invalid, setInvalid] = React.useState(false);
return ( <Field invalid={invalid} className="w-fit"> <FieldLabel>Sign-in code</FieldLabel> <OTPInput length={6} value={code} onValueChange={(value) => { setCode(value); setInvalid(false); }} onValueComplete={(value) => { if (value === "424242") { setCode(""); toast.add({ title: "Code verified", type: "success" }); } else { setInvalid(true); } }} /> {invalid ? ( <FieldError>That code didn't work. Check it and try again.</FieldError> ) : ( <FieldDescription>Try 424242, or any other code to see an error.</FieldDescription> )} </Field> );}Groups
groupSize splits a long code into groups of that many boxes, with a separator between them.
import { Field, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputGrouped() { return ( <Field className="w-fit"> <FieldLabel>Authenticator code</FieldLabel> <OTPInput length={6} groupSize={3} /> </Field> );}Composing the boxes
For groups that groupSize can't make, give OTPInput an OTPInputSlot for each character,
arranged in OTPInputGroups with an OTPInputSeparator between them. In this example, a TV's
pairing code is split into a group of three characters and a group of four.
Enter the code shown on your TV, such as ABC-DEFG.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";import { OTPInput, OTPInputGroup, OTPInputSeparator, OTPInputSlot,} from "@/components/ui/otp-input";
export default function OTPInputDeviceCode() { return ( <Field className="w-fit"> <FieldLabel>TV code</FieldLabel> <OTPInput length={7} validationType="alphanumeric" normalizeValue={(value) => value.toUpperCase()} > <OTPInputGroup> <OTPInputSlot /> <OTPInputSlot /> <OTPInputSlot /> </OTPInputGroup> <OTPInputSeparator /> <OTPInputGroup> <OTPInputSlot /> <OTPInputSlot /> <OTPInputSlot /> <OTPInputSlot /> </OTPInputGroup> </OTPInput> <FieldDescription>Enter the code shown on your TV, such as ABC-DEFG.</FieldDescription> </Field> );}Large
Set size="lg" on OTPInput for larger boxes, or size="sm" for smaller ones.
import { Field, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputLarge() { return ( <Field className="w-fit"> <FieldLabel>Device code</FieldLabel> <OTPInput length={6} size="lg" groupSize={3} /> </Field> );}Letters and numbers
OTPInput accepts only digits by default. Set validationType="alphanumeric" to allow letters, and
normalizeValue to transform what's typed, such as converting it to uppercase.
Letters are converted to uppercase.
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputAlphanumeric() { return ( <Field className="w-fit"> <FieldLabel>Recovery code</FieldLabel> <OTPInput length={8} validationType="alphanumeric" normalizeValue={(value) => value.toUpperCase()} groupSize={4} /> <FieldDescription>Letters are converted to uppercase.</FieldDescription> </Field> );}Masked
mask hides the characters, like a password field.
import { Field, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputMasked() { return ( <Field className="w-fit"> <FieldLabel>PIN</FieldLabel> <OTPInput length={4} mask /> </Field> );}Disabled
import { Field, FieldLabel } from "@/components/ui/field";import { OTPInput } from "@/components/ui/otp-input";
export default function OTPInputDisabled() { return ( <Field disabled className="w-fit"> <FieldLabel>Previous PIN</FieldLabel> <OTPInput length={4} defaultValue="4821" mask /> </Field> );}Props
Base UI's styling (opens in a new tab) and composition (opens in a new tab) guides say more about className, style and render.
OTPInput
| Prop | Type | Default |
|---|---|---|
idThe id of the first input element.
Subsequent inputs derive their ids from it ( | string | — |
autoCompleteThe input autocomplete attribute. Applied to the first slot and hidden validation input. | string | 'one-time-code' |
formA string specifying the | string | — |
lengthThe number of OTP input slots. Required so the root can clamp values, detect completion, and generate consistent validation markup before all slots hydrate. | number | — |
autoSubmitWhether to submit the owning form when the OTP becomes complete. | boolean | false |
maskWhether the slot inputs should mask entered characters.
Pass | boolean | false |
inputModeThe virtual keyboard hint applied to the slot inputs and hidden validation input. Built-in validation modes provide sensible defaults, but you can override them when needed. | "decimal" | "email" | "none" | "numeric" | "search" | "tel" | "text" | "url" | — |
validationTypeThe type of input validation to apply to the OTP value. | "alpha" | "alphanumeric" | "none" | "numeric" | 'numeric' |
normalizeValueFunction that normalizes the OTP value after whitespace and The returned value is filtered by | ((value: string) => string) | — |
requiredWhether the user must enter a value before submitting a form. | boolean | false |
disabledWhether the component should ignore user interaction. | boolean | false |
readOnlyWhether the user should be unable to change the field value. | boolean | false |
nameIdentifies the field when a form is submitted. | string | — |
valueThe OTP value. | string | — |
defaultValueThe uncontrolled OTP value when the component is initially rendered. | string | — |
onValueChangeCallback fired when the OTP value changes. The
| ((value: string, eventDetails: OTPField.Root.ChangeEventDetails) => void) | — |
onValueInvalidCallback fired when entered text contains characters that are rejected by validation or normalization before the OTP value updates. The | ((value: string, eventDetails: OTPField.Root.InvalidEventDetails) => void) | — |
onValueCompleteCallback function that is fired when the OTP value becomes complete, or when a complete value is pasted while the OTP is already complete. When the value changes, it runs later than If | ((value: string, eventDetails: OTPField.Root.CompleteEventDetails) => void) | — |
sizeThe size of each slot: | "default" | "lg" | "sm" | "default" |
groupSizeSplits the boxes into groups of this many, with a separator between groups. Applies only when | number | — |
classNameCSS class applied to the element, or a function that returns a class based on the component's state. | string | ((state: OTPFieldRootState) => string | undefined) | — |
styleStyle applied to the element, or a function that returns a style object based on the component's state. | React.CSSProperties | ((state: OTPFieldRootState) => React.CSSProperties | undefined) | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<HTMLProps, OTPFieldRootState> | React.ReactElement | — |
OTPInputGroup
| Prop | Type | Default |
|---|---|---|
classNameCSS class applied to the element. | string | — |
styleStyle applied to the element. | React.CSSProperties | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<HTMLProps, {}> | React.ReactElement | — |
OTPInputSlot
| Prop | Type | Default |
|---|---|---|
classNameCSS class applied to the element, or a function that returns a class based on the component's state. | string | ((state: OTPFieldInputState) => string | undefined) | — |
styleStyle applied to the element, or a function that returns a style object based on the component's state. | React.CSSProperties | ((state: OTPFieldInputState) => React.CSSProperties | undefined) | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, OTPFieldInputState> | React.ReactElement | — |
OTPInputSeparator
| Prop | Type | Default |
|---|---|---|
orientationThe orientation of the separator. | "horizontal" | "vertical" | 'horizontal' |
classNameCSS class applied to the element, or a function that returns a class based on the component's state. | string | ((state: SeparatorState) => string | undefined) | — |
styleStyle applied to the element, or a function that returns a style object based on the component's state. | React.CSSProperties | ((state: SeparatorState) => React.CSSProperties | undefined) | — |
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a | ComponentRenderFn<HTMLProps, SeparatorState> | React.ReactElement | — |