Skip to content

OTP Input

A one-time code in separate slots: paste fills them all, typing moves forward, backspace moves back.

On this page
Used for
  • Email sign-in code
  • Two-factor check
  • Confirm an invite
  • Approve an agent action

Examples

Installation

npx knowns-ui@latest add otp-input
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.

Props

length
number
Default6
Number of slots.
value / defaultValue / onValueChange
string
Default""
The code so far.
onComplete
(code) => void
Fires when every slot is filled.
onVerify
(code, signal) => boolean | Promise<boolean>
Checks a complete code. While it runs the slots slide together into one pill; true turns it green with a check and the label; false or a throw shakes it once and splits it back with the code cleared and marked invalid. The signal aborts when the code changes or the input unmounts.
status / onStatusChange
"idle" | "verifying" | "verified" | "error"
Default"idle"
The verify state, to control it yourself or to follow it (for a message under the field).
verifiedLabel
string
Default"Verified"
Words in the green pill.
mode
"numeric" | "alphanumeric"
Default"numeric"
Accepted characters; letters are upper-cased. Sets inputMode for the right mobile keyboard.
mask
boolean
Defaultfalse
Dots instead of characters.
groups
number[]
Gaps after these slot counts, e.g. [3].
invalid / disabled
boolean
Defaultfalse
Red ring / not editable. Inside a Field with an error, invalid is set for you.
size
"md" | "lg"
Default"md"
40 or 48px slots.
Motion
-
A typed character drops 6px into its slot (180ms); one focus tint slides to the active slot (120ms); a caret blinks in the empty active slot; merging, the slots slide together and square their inner corners (180ms); an error shakes once (280ms). Reduced motion: all instant, the caret steady.
id / name
string
id goes on the first slot (for Field); name adds a hidden input with the whole code.

Keyboard: type to fill and advance, Backspace clears and steps back, ← → Home End move. Pasting anywhere fills from that slot (a full-length code fills from the first). The first slot has autocomplete="one-time-code", so phones offer the code from SMS.

What it handles

  1. 01Splits a code across length slots (6 by default), each a real input that takes one character and moves on.
  2. 02Fills every slot from one paste, starting at the first slot when the pasted text is as long as the whole code.
  3. 03Backspace clears the slot, or the one before when it is already empty; Delete, ArrowLeft, ArrowRight, Home and End move around the row.
  4. 04Accepts digits in numeric mode and letters plus digits in alphanumeric mode, upper-casing the latter and dropping anything else.
  5. 05Calls onComplete once, when the last slot fills, and sets one-time-code autocomplete on the first slot so a phone can offer the SMS code.
  6. 06Masks characters with mask, draws a gap after the slot counts in groups, and marks every slot invalid together through invalid.

States

First render
Nothing. The slots paint empty or with the value
none
Focus
The slot gets a ring and its text is selected
180ms, ease out
Typing a character
It lands in the slot and focus moves to the next one
none, at once
Invalid
Every slot swaps its ring to red
180ms, ease out
Reduced motion
The ring still changes colour, with no other motion to remove
none

Works with

When to use

When to use

  • One-time codes from email or SMS, with paste, auto-advance and an error state.

When not to use

Using with AI

Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:

  • Add the Knowns UI OTP Input to my app and wire it to my own data.
  • I need one-time codes from email or SMS, with paste, auto-advance and an error state. Set that up with the Knowns UI OTP Input.
  • Which OTP Input props do I need for a minimal setup? Show me a short example.

With the server connected, your assistant calls get_component({ name: "otp-input" }) and search_docs({ query: "otp input" }).

Need help?

Stuck on OTP Input, or found something off? We read every message.