Skip to content

OTP Input

A one-time code with a box for each character.

Enter the 6-digit code we sent to tony@example.com.

Installation

  1. Add the registryOnce per project
  2. Add otp-input

Usage

OTPInput renders a box for each of its length characters. In a Field, the FieldLabel labels the first box, and screen readers announce each of the others by its position, such as "Character 2 of 4". To change that wording, lay out the boxes yourself and give each OTPInputSlot an aria-label.

To lay out the boxes yourself, pass OTPInput the parts as children, as in Composing the boxes.

Composition

Examples

Checking the code

onValueComplete is called once every box is filled. When the code is wrong, set invalid on the Field to mark every box invalid, and show a FieldError.

Try 424242, or any other code to see an error.

Groups

groupSize splits a long code into groups of that many boxes, with a separator between them.

Composing the boxes

For groups that groupSize can't make, give OTPInput an OTPInputSlot for each character, arranged in OTPInputGroups with an OTPInputSeparator between them. In this example, a TV's pairing code is split into a group of three characters and a group of four.

Enter the code shown on your TV, such as ABC-DEFG.

Large

Set size="lg" on OTPInput for larger boxes, or size="sm" for smaller ones.

Letters and numbers

OTPInput accepts only digits by default. Set validationType="alphanumeric" to allow letters, and normalizeValue to transform what's typed, such as converting it to uppercase.

Letters are converted to uppercase.

Masked

mask hides the characters, like a password field.

Disabled

Props

Base UI's styling (opens in a new tab) and composition (opens in a new tab) guides say more about className, style and render.

OTPInput

OTPInputGroup

OTPInputSlot

OTPInputSeparator