# OTP field

Source: https://ecosphere.dev/docs/forms/otp-field (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

A one-time code, one box per digit, in four, five or six digits.

`import { EcoOtpField } from "vue-ecosphere"`

## Six digits

Typing advances, Backspace goes back, and pasting a code fills every box.

Vue (vue-ecosphere):

```vue
<EcoOtpField v-model="code" label="Verification code"
  hint="We sent it to +91 98••• •••21." @complete="(c) => (completed = c)" first />
```

React (react-ecosphere):

```tsx
const [code, setCode] = useState("");
const [completed, setCompleted] = useState("");

<EcoOtpField value={code} onChange={setCode} label="Verification code"
  hint="We sent it to +91 98••• •••21." onComplete={setCompleted} first />
```

## Four digits

Vue (vue-ecosphere):

```vue
<EcoOtpField v-model="pin" label="PIN" :length="4" first />
```

React (react-ecosphere):

```tsx
const [pin, setPin] = useState("4289");

<EcoOtpField value={pin} onChange={setPin} label="PIN" length={4} first />
```

## Five, grouped

Vue (vue-ecosphere):

```vue
<EcoOtpField v-model="access" label="Access code" :length="5" size="sm" :group-after="2" first />
```

React (react-ecosphere):

```tsx
const [access, setAccess] = useState("42891");

<EcoOtpField value={access} onChange={setAccess} label="Access code" length={5} size="sm" groupAfter={2} first />
```

## Wrong code

Always offer a way to send another.

Vue (vue-ecosphere):

```vue
<EcoOtpField v-model="wrong" label="Verification code"
  error="That code has expired. Send a new one." first />
```

React (react-ecosphere):

```tsx
const [wrong, setWrong] = useState("428913");

<EcoOtpField value={wrong} onChange={setWrong} label="Verification code"
  error="That code has expired. Send a new one." first />
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `string` | "" | The digits entered so far. |
| `length` | `4 \| 5 \| 6` | 6 | One box per digit, so the length is visible before typing. |
| `size` | `"md" \| "sm"` | "md" | Smaller boxes (otp--sm). |
| `groupAfter` | `number` | — | Put an otp__sep after this many digits. |
| `error / invalid` | `string / boolean` | — | The whole group turns is-invalid: a wrong code is wrong as a whole. |
| `label / required / hint / first / disabled` | `—` | — | Field props. Say where the code went in the hint, masked. |
| `groupLabel` | `string` | "Verification code" | Group name when there is no label. |
| `@complete` | `(code: string) => void` | — | Every box is filled (typing or paste). |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value / onChange` | `string` | "" | The digits entered so far. |
| `length` | `4 \| 5 \| 6` | 6 | One box per digit, so the length is visible before typing. |
| `size` | `"md" \| "sm"` | "md" | Smaller boxes (otp--sm). |
| `groupAfter` | `number` | — | Put an otp__sep after this many digits. |
| `error / invalid` | `string / boolean` | — | The whole group turns is-invalid: a wrong code is wrong as a whole. |
| `label / required / hint / first / disabled` | `—` | — | Field props. Say where the code went in the hint, masked. |
| `groupLabel` | `string` | "Verification code" | Group name when there is no label. |
| `onComplete` | `(code: string) => void` | — | Every box is filled (typing or paste). |
