

<InteractiveDemo
  name="otp-field"
  controls="[
  {
    type: &#x22;string&#x22;,
    name: &#x22;label&#x22;,
    defaultValue: &#x22;Verification code&#x22;,
  },
  {
    type: &#x22;string&#x22;,
    name: &#x22;description&#x22;,
    defaultValue: &#x22;&#x22;,
    placeholder: &#x22;Helper text&#x22;,
  },
  {
    type: &#x22;string&#x22;,
    name: &#x22;errorMessage&#x22;,
    defaultValue: &#x22;&#x22;,
    placeholder: &#x22;Error text&#x22;,
  },
  {
    name: &#x22;length&#x22;,
    options: [4, 5, 6, 7, 8],
  },
  &#x22;mask&#x22;,
  &#x22;isDisabled&#x22;,
  &#x22;isReadOnly&#x22;,
  &#x22;isInvalid&#x22;,
]"
/>

## Installation [#installation]

<InstallPrerequisite>
  Before adding components, init your project with your design system from the [Studio](/studio).
</InstallPrerequisite>

<CodeBlockTabs defaultValue="npm" groupId="package-manager">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="npm">
      npm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="pnpm">
      pnpm
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="yarn">
      yarn
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="bun">
      bun
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="npm">
    ```bash
    npx shadcn@latest add @dotui/otp-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="pnpm">
    ```bash
    pnpm dlx shadcn@latest add @dotui/otp-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="yarn">
    ```bash
    yarn dlx shadcn@latest add @dotui/otp-field
    ```
  </CodeBlockTab>

  <CodeBlockTab value="bun">
    ```bash
    bunx shadcn@latest add @dotui/otp-field
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Usage [#usage]

Use `OTPField` with `OTPFieldGroup` and the existing `Input` component to compose passcode slots.

```tsx
import { OTPField, OTPFieldGroup } from "@/components/ui/otp-field"
import { Label } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
```

```tsx
<OTPField length={6}>
  <Label>Verification code</Label>
  <OTPFieldGroup>
    <Input />
    <Input aria-label="Digit 2" />
    <Input aria-label="Digit 3" />
    <Input aria-label="Digit 4" />
    <Input aria-label="Digit 5" />
    <Input aria-label="Digit 6" />
  </OTPFieldGroup>
</OTPField>
```

## Anatomy [#anatomy]

`OTPField` wraps a `Label`, one or more `OTPFieldGroup`s of single-character `Input` slots, and optional `Description`, `FieldError`, and `OTPFieldSeparator` parts. `length` sets how many slots render. Split the slots across `OTPFieldGroup`s with an `OTPFieldSeparator` between them.

```tsx
import {
  OTPField,
  OTPFieldGroup,
  OTPFieldSeparator,
} from "@/components/ui/otp-field"
import { Description, FieldError, Label } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
```

```tsx
<OTPField length={6}>
  <Label />
  <OTPFieldGroup>
    <Input />
  </OTPFieldGroup>
  <OTPFieldSeparator />
  <OTPFieldGroup>
    <Input />
  </OTPFieldGroup>
  <Description />
  <FieldError />
</OTPField>
```

## Value [#value]

The value is a string sized by `length`. Pass `defaultValue` for uncontrolled state, or `value` with `onChange` to control it.

```tsx
const [value, setValue] = React.useState('')

<OTPField length={6} value={value} onChange={setValue}>
  {/* ... */}
</OTPField>
```

## Validation [#validation]

Mark the field `isRequired`, drive `isInvalid` to surface a `FieldError` alongside a `Description`, and set `name` for form submission. Use `validationType` to restrict input to `"numeric"` (default) or `"alphanumeric"`.

```tsx
<OTPField
  length={6}
  name="code"
  isInvalid={isInvalid}
  validationType="alphanumeric"
>
  <Label>Recovery code</Label>
  <OTPFieldGroup>
    <Input />
    {/* ... */}
  </OTPFieldGroup>
  <Description>Enter the code we sent you.</Description>
  <FieldError>Enter all six characters.</FieldError>
</OTPField>
```

## Examples [#examples]

<Examples className="sm:grid-cols-2">
  <Example name="otp-field/demos/basic" title="Basic OTP Field" />

  <Example name="otp-field/demos/form" title="Verification Form" />

  <Example name="otp-field/demos/two-factor-auth" title="2FA Verification" />

  <Example name="otp-field/demos/email-verification" title="Email Verification" />
</Examples>

## API Reference [#api-reference]

### OTPField [#otpfield]

<Reference name="otp-field" />

### OTPFieldGroup [#otpfieldgroup]

<Reference name="otp-field-group" />

### OTPFieldSeparator [#otpfieldseparator]

<Reference name="otp-field-separator" />
