Skip to contents

OTP Field

A one-time password input composed of individual character slots.

View as Markdown

Note: OTP Field is currently in preview. Its API may change before it becomes stable.

Enter the 6-character code we sent to your device.

Usage guidelines

Anatomy

Import the component and assemble its parts:

Anatomy

Examples

Labeling an OTP field

Pass an id to <OTPField.Root> and use a native <label> with a matching htmlFor. Let the first input use the field label, and add aria-label to the remaining inputs so assistive technology can announce which slot is focused.

Optionally, add aria-describedby when supporting text should be announced with the field.

OTP Field with a native label and description

Form integration

Use Field to handle label associations and form integration:

Using OTP Field in a form

Pass autoSubmit to submit the owning form automatically when all slots are filled, or use onValueComplete to react to completion without submitting.

Alphanumeric verification codes

Use validationType="alphanumeric" for recovery, backup, or invite codes that mix letters and numbers.

Accept letters and numbers for backup codes such as A7C9XZ.

Grouped layouts

Wrap subsets of inputs in your own layout elements and use <OTPField.Separator> when you want the code presented in smaller visual chunks such as 123-456.

Placeholder hints

<OTPField.Input> is a real input, so native placeholder props and CSS work as usual. This example keeps placeholder hints visible until the active slot receives focus.

Placeholder hints can stay visible until the active slot is focused.

Custom sanitization

Set validationType="none" with sanitizeValue when you need to normalize pasted values before they reach state or apply custom validation rules. Use inputMode when a custom rule still needs a specific virtual keyboard hint, and onValueInvalid when you want to react to rejected characters.

Digits 0-3 only.

Masked entry

Use mask when the code should be obscured while it is being typed.

Use mask to obscure the code on shared screens.

API reference

Root

Groups all OTP field parts and manages their state. Renders a <div> element.

namestring
Name
Description

Identifies the field when a form is submitted.

Type
defaultValuestring
Description

The uncontrolled OTP value when the component is initially rendered.

Type
valuestring
Name
Description

The OTP value.

Type
onValueChangefunction
Description

Callback fired when the OTP value changes.

The eventDetails.reason indicates what triggered the change:

  • 'input-change' for typing or autofill
  • 'input-clear' when a character is removed by text input
  • 'input-paste' for paste interactions
  • 'keyboard' for keyboard interactions that change the value
Type
autoCompletestring'one-time-code'
Description

The input autocomplete attribute. Applied to the first slot and hidden validation input.

Type
Default
'one-time-code'
autoSubmitbooleanfalse
Description

Whether to submit the owning form when the OTP becomes complete.

Type
Default
false
formstring
Name
Description

A string specifying the form element with which the hidden input is associated. This string’s value must match the id of a form element in the same document.

Type
inputModeUnion
Description

The virtual keyboard hint applied to the slot inputs and hidden validation input.

Built-in validation modes provide sensible defaults, but you can override them when needed.

Type
length*
Name
Description

The number of OTP input slots. Required so the root can clamp values, detect completion, and generate consistent validation markup before all slots hydrate.

Type
maskbooleanfalse
Name
Description

Whether the slot inputs should mask entered characters. Pass type directly to individual <OTPField.Input> parts to use a custom input type.

Type
Default
false
onValueCompletefunction
Description

Callback function that is fired when the OTP value becomes complete.

It runs later than onValueChange, after the internal value update is applied.

If autoSubmit is enabled, it runs immediately before the owning form is submitted.

Type
onValueInvalidfunction
Description

Callback fired when entered text contains characters that are rejected by sanitization, before the OTP value updates.

The value argument is the attempted user-entered string before sanitization.

Type
sanitizeValuefunction
Description

Function for custom sanitization when validationType is set to 'none'. This function runs before updating the OTP value from user interactions.

Type
validationTypeOTPFieldPreview.Root.ValidationType'numeric'
Description

The type of input validation to apply to the OTP value.

Default
'numeric'
disabledbooleanfalse
Description

Whether the component should ignore user interaction.

Type
Default
false
readOnlybooleanfalse
Description

Whether the user should be unable to change the field value.

Type
Default
false
requiredbooleanfalse
Description

Whether the user must enter a value before submitting a form.

Type
Default
false
idstring
Name
Description

The id of the first input element. Subsequent inputs derive their ids from it ({id}-2, {id}-3, and so on).

Type
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the OTP field is disabled.

data-readonly

Present when the OTP field is readonly.

data-required

Present when the OTP field is required.

data-valid

Present when the OTP field is in valid state (when wrapped in Field.Root).

data-invalid

Present when the OTP field is in invalid state (when wrapped in Field.Root).

data-dirty

Present when the OTP field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the OTP field has been touched (when wrapped in Field.Root).

data-complete

Present when all slots are filled.

data-filled

Present when the OTP field contains at least one character.

data-focused

Present when one of the OTP field inputs is focused.

AttributeDescription
data-disabled

Present when the OTP field is disabled.

data-readonly

Present when the OTP field is readonly.

data-required

Present when the OTP field is required.

data-valid

Present when the OTP field is in valid state (when wrapped in Field.Root).

data-invalid

Present when the OTP field is in invalid state (when wrapped in Field.Root).

data-dirty

Present when the OTP field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the OTP field has been touched (when wrapped in Field.Root).

data-complete

Present when all slots are filled.

data-filled

Present when the OTP field contains at least one character.

data-focused

Present when one of the OTP field inputs is focused.

OTPFieldPreview.Root.PropsHide

Re-Export of Root props as OTPFieldPreviewRootProps

OTPFieldPreview.Root.StateHide

OTPFieldPreview.Root.ChangeEventReasonHide

OTPFieldPreview.Root.ChangeEventDetailsHide

OTPFieldPreview.Root.CompleteEventDetailsHide

OTPFieldPreview.Root.CompleteEventReasonHide

OTPFieldPreview.Root.InvalidEventDetailsHide

OTPFieldPreview.Root.InvalidEventReasonHide

OTPFieldPreview.Root.ValidationTypeHide

Input

An individual OTP character input. Renders an <input> element.

classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-disabled

Present when the OTP field is disabled.

data-readonly

Present when the OTP field is readonly.

data-required

Present when the OTP field is required.

data-valid

Present when the OTP field is in valid state (when wrapped in Field.Root).

data-invalid

Present when the OTP field is in invalid state (when wrapped in Field.Root).

data-dirty

Present when the OTP field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the OTP field has been touched (when wrapped in Field.Root).

data-complete

Present when all slots are filled.

data-filled

Present when the input contains a character.

data-focused

Present when any OTP field input is focused.

AttributeDescription
data-disabled

Present when the OTP field is disabled.

data-readonly

Present when the OTP field is readonly.

data-required

Present when the OTP field is required.

data-valid

Present when the OTP field is in valid state (when wrapped in Field.Root).

data-invalid

Present when the OTP field is in invalid state (when wrapped in Field.Root).

data-dirty

Present when the OTP field’s value has changed (when wrapped in Field.Root).

data-touched

Present when the OTP field has been touched (when wrapped in Field.Root).

data-complete

Present when all slots are filled.

data-filled

Present when the input contains a character.

data-focused

Present when any OTP field input is focused.

OTPFieldPreview.Input.PropsHide

Re-Export of Input props as OTPFieldPreviewInputProps

OTPFieldPreview.Input.StateHide

Separator

A separator element accessible to screen readers. Renders a <div> element.

orientationOrientation'horizontal'
Description

The orientation of the separator.

Type
Default
'horizontal'
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type

OTPFieldPreview.Separator.PropsHide

Re-Export of Separator props as OTPFieldPreviewSeparatorProps

OTPFieldPreview.Separator.StateHide