This page as markdown: /otp-field.md · Index for agents: /llms.txt · Everything in one file: /llms-full.txt

OtpField

Status: stable

One-time-code entry over Base UI's OTP Field — one slot per character, SMS autofill on the first slot, paste fills every slot, and only one tab stop for the whole code.

Keywords: otp, one-time code, one-time password, verification code, 2fa, two-factor, mfa, pin, passcode, input otp

Usage

Use OtpField for a short, fixed-length code the user copies from somewhere else — an SMS or email verification code, an authenticator app, a device pairing PIN. The slots show how long the code is and where the user is in it, which a plain Input cannot.

Best practices

Anatomy

Part Required Description
Root yes A role=group that holds the value, carries the field label and description, and reports completion.
Slot yes One input per character. The first carries autocomplete="one-time-code" and accepts the whole code.
Group gap no A decorative dash inserted every groupSize slots; hidden from assistive tech.

Props

Name Type Default Description
length number 6 Number of character slots. Completion fires when every slot is filled.
groupSize number — Insert a visual gap every N slots (3 renders 123 – 456). The value stays one contiguous string.
value / defaultValue string — Controlled / uncontrolled code.
onValueChange (value: string, details) => void — Fires on every edit. details.reason is input-change, input-clear, input-paste or keyboard.
onValueComplete (value: string, details) => void — Fires once every slot is filled — the place to verify the code. Also fires when a complete code is pasted over a complete value.
onValueInvalid (attempted: string, details) => void — Fires when typed or pasted text contains characters validationType rejects, so you can explain the rejection rather than letting it vanish.
validationType "numeric" | "alpha" | "alphanumeric" | "none" "numeric" Which characters a slot accepts. Also picks the virtual keyboard (numeric → number pad).
autoSubmit boolean false Submit the owning form when the code becomes complete.
mask boolean false Render slots as password inputs. Only for codes that are secret on screen, such as a card PIN.
getSlotLabel (index: number, length: number) => string Character N of L Accessible name for slots after the first. Override to localize.
aria-invalid boolean — Marks every slot invalid (destructive border and digits). Turning it on after mount shakes the field once. <Field error> sets it for you.
motion "full" | "subtle" | "off" "full" How much motion this component uses: full (default) inherits the app's policy, subtle keeps feedback and drops the flourish, off stills it. It can only turn motion down — it never overrides a reader's reduce-motion setting. The shake is the flourish; the destructive colour stays at every level.
disabled / readOnly / required / name / id / form — — Standard form-control props. id lands on the first slot, so <Field> and <label htmlFor> name the field.

Tokens used

field-border, field, foreground, muted-foreground, border-strong, primary, ring, destructive

Motion

Accessibility

Related

Examples

Six digits

import { Field, OtpField } from "@sableui/react"

<Field label="Verification code"><OtpField length={6} onValueComplete={verify} /></Field>

In a field, grouped

import { Field, OtpField } from "@sableui/react"

<Field label="Verification code" description="Enter the 6-digit code we sent to your phone."><OtpField length={6} groupSize={3} /></Field>

With an error

import { Field, OtpField } from "@sableui/react"

<Field label="Verification code" error="That code has expired. Request a new code and try again."><OtpField length={6} groupSize={3} defaultValue="482913" /></Field>

Verify on completion, shake on a wrong code

import { Field, OtpField } from "@sableui/react"

const [code, setCode] = useState("")
const [wrong, setWrong] = useState(false)
<Field label="Verification code" error={wrong ? "That code does not match. Check the message and try again." : undefined}><OtpField length={6} groupSize={3} value={code} onValueChange={(next) => { setCode(next); setWrong(false) }} onValueComplete={(value) => setWrong(value !== expected)} /></Field>

Alphanumeric

import { Field, OtpField } from "@sableui/react"

<Field label="Pairing code"><OtpField length={8} groupSize={4} validationType="alphanumeric" /></Field>