# Input OTP

Accessible one-time password input with configurable length and pattern.

> For the complete documentation index, see [llms.txt](/llms.txt). Markdown variants are available by appending `.md` to any URL or sending an `Accept: text/markdown` header. An agent skill is available at [/.well-known/agent-skills/site-skill.md](/.well-known/agent-skills/site-skill.md).

<DocsBaseSwitcher base="web" component="input-otp" />

<ComponentPreview name="input-otp" title="input-otp.tsx">
<Component />
</ComponentPreview>

Input OTP renders a one-time password field with real input handling for paste, backspace, and arrow keys. Each slot is a separate visual box, but the underlying input is a single element for form submission.

## Installation [#installation]

<CodeTabs>
<TabsList>
<TabsTrigger value="cli">
Command
</TabsTrigger>

<TabsTrigger value="manual">
Manual
</TabsTrigger>
</TabsList>

<TabsContent value="cli">
~~~bash
npx frakment add input-otp
~~~
</TabsContent>

<TabsContent value="manual">
<Steps>
<Step>
Install the following dependency:
</Step>

~~~bash
npm install lucide-react
~~~

<Step>
Copy and paste the following code into your project.
</Step>

<ComponentSource name="input-otp" title="components/ui/input-otp.tsx" />

<Step>
Update the import paths to match your project setup.
</Step>
</Steps>
</TabsContent>
</CodeTabs>

## Usage [#usage]

~~~tsx title="input-otp.tsx"
import {
  InputOTP,
  InputOTPGroup,
  InputOTPSeparator,
  InputOTPSlot,
} from "@/components/ui/input-otp";
~~~

~~~tsx
<InputOTP maxLength={6}>
  <InputOTPGroup>
    <InputOTPSlot index={0} />
    <InputOTPSlot index={1} />
    <InputOTPSlot index={2} />
  </InputOTPGroup>
  <InputOTPSeparator />
  <InputOTPGroup>
    <InputOTPSlot index={3} />
    <InputOTPSlot index={4} />
    <InputOTPSlot index={5} />
  </InputOTPGroup>
</InputOTP>
~~~

## Examples [#examples]

### Pattern [#pattern]

`digits` restricts input to numbers.

<ComponentPreview name="input-otp-pattern" src="examples/input-otp-pattern.tsx" title="input-otp-pattern.tsx">
<InputOTPPattern />
</ComponentPreview>

### With separator [#with-separator]

<ComponentPreview name="input-otp-separator" src="examples/input-otp-separator.tsx" title="input-otp-separator.tsx">
<h />
</ComponentPreview>

### Controlled [#controlled]

Drive the value from your own state.

<ComponentPreview name="input-otp-controlled" src="examples/input-otp-controlled.tsx" title="input-otp-controlled.tsx" previewClassName="h-auto py-8">
<InputOTPControlled />
</ComponentPreview>

### Invalid [#invalid]

Set `aria-invalid` on slots to mark them invalid.

<ComponentPreview name="input-otp-invalid" src="examples/input-otp-invalid.tsx" title="input-otp-invalid.tsx">
<InputOTPInvalid />
</ComponentPreview>

### Alphanumeric [#alphanumeric]

`alphanumeric` allows letters and numbers.

<ComponentPreview name="input-otp-alphanumeric" src="examples/input-otp-alphanumeric.tsx" title="input-otp-alphanumeric.tsx">
<InputOTPAlphanumeric />
</ComponentPreview>

### Disabled [#disabled]

<ComponentPreview name="input-otp-disabled" src="examples/input-otp-disabled.tsx" title="input-otp-disabled.tsx">
<p />
</ComponentPreview>

### Four digits [#four-digits]

<ComponentPreview name="input-otp-four-digits" src="examples/input-otp-four-digits.tsx" title="input-otp-four-digits.tsx">
<InputOTPFourDigits />
</ComponentPreview>

## In Context [#in-context]

### Verification code [#verification-code]

<ComponentPreview name="input-otp-verification" src="examples/input-otp-verification.tsx" title="input-otp-verification.tsx">
<r />
</ComponentPreview>

## API Reference [#api-reference]

### InputOTP [#inputotp]

<PropsTable rows={[{"name":"maxLength","type":"number","description":"Number of character slots."},{"name":"value","type":"string","description":"Controlled value."},{"name":"onChange","type":"function","description":"Called with the new value."},{"name":"pattern","type":"string","description":"Regex string that limits allowed characters."},{"name":"disabled","type":"boolean","default":"false","description":"Prevent input."},{"name":"containerClassName","type":"string","description":"Applied to the input container."}]} />

### InputOTPSlot [#inputotpslot]

<PropsTable rows={[{"name":"index","type":"number","description":"Position of this slot within the input."}]} />

## Controlled [#controlled-1]

```tsx
const [value, setValue] = React.useState("")
<InputOTP value={value} onChange={setValue} maxLength={6}>{/* ... */}</InputOTP>
```

## States [#states]

| State | How |
| :-- | :-- |
| Empty | default |
| Partial | some slots filled |
| Complete | all slots filled |
| Invalid | `aria-invalid="true"` |

## Keyboard [#keyboard]

| Key | Action |
| :-- | :-- |
| `←` / `→` | Move between slots |
| `Backspace` | Delete and move back |
| `Ctrl + V` / `Cmd + V` | Paste the full code |

## Data Attributes [#data-attributes]

| Attribute | Values |
| :-- | :-- |
| `data-slot` | `input-otp`, `input-otp-group`, `input-otp-slot`, `input-otp-separator` |
| `data-active` | `true` (caret in this slot) |

## Accessibility [#accessibility]

- The wrapper renders a real `<input>` with `inputmode="numeric"` for mobile keyboards.
- Slots expose `data-active` and `data-invalid` for styling, but the input itself carries `aria-invalid`.
- Paste, backspace, and arrow navigation are handled automatically.
- Pair with `FieldLabel htmlFor` and `id` for labelled forms.

## Best Practices [#best-practices]

- **Use `maxLength` matching the code length.** Do not let the input grow arbitrarily.
- **Group visually, not semantically.** 3 + 3 reads better than 6 in one row.
- **Auto-focus the first slot on mount.** Reduces taps on mobile.
- **Paste support is required.** Users copy codes from messages; paste should fill every slot.

## Related Components [#related-components]

- [Input](/docs/components/input) — plain text
- [Field](/docs/components/field) — label + error