Skip to contentSkip to content

OTPField API

API reference docs for the React OTPField component. Learn about the props, CSS, and other APIs of this exported module.

Demos

Import

import OTPField from '@mui/material/OTPField';
// or
import { OTPField } from '@mui/material';

Learn about the difference by reading this guide on minimizing bundle size.



A field for entering a one-time password, one character per input.

Props

Props of the native component are also available.

NameTypeDefaultDescription
aria-describedbystring-

Identifies the element (or elements) that describes the object.

aria-labelstring-

Defines a string value that labels the current element.

aria-labelledbystring-

Identifies the element (or elements) that labels the current element.

autoCompletestring'one-time-code'

The autocomplete hint for the first character input and the form input.

autoFocusbool-

Focus the first character input on mount.

autoSubmitboolfalse

Submit the associated form when the code becomes complete.

childrennode-

Character inputs and optional decorative separators. When omitted, the field renders one OTPFieldInput per character.

classesobject-

Override or extend the styles applied to the component.

See CSS classes API below for more details.

color'primary'
| 'secondary'
| 'error'
| 'info'
| 'success'
| 'warning'
| string
'primary'

The theme palette color of the focused character's outline. To use a custom palette color in TypeScript, augment OTPFieldPropsColorOverrides.

defaultValuestring''

The initial code for an uncontrolled field.

disabledbool-

Disable all the character inputs and omit the code from form submission.

errorboolfalse

Display the field in an error state.

formstring-

The ID of the form that owns the field.

fullWidthboolfalse

If true, the field fills its container and the character inputs share the available width equally.

getInputAriaLabelfuncfunction defaultGetInputAriaLabel(position: number, length: number) { return `Character ${position} of ${length}`; }

Accessible name for a character after the first. Positions start at one. For localization purposes, you can use the provided translations.

helperTextnode-

The supporting or error text displayed below the inputs.

idstring-

The ID of the first character input. Subsequent input IDs are derived from it.

inputMode'decimal'
| 'email'
| 'none'
| 'numeric'
| 'search'
| 'tel'
| 'text'
| 'url'
-

The virtual keyboard hint. Defaults to the keyboard for the validation type.

inputReffunc
| { current?: object }
-

A ref to the first visible character input.

labelnode-

The label associated with the first character input.

lengthnumber6

The number of characters in the code. When passing children, match the number of OTPFieldInput elements.

maskboolfalse

Mask the characters as they are entered.

namestring-

The name used to submit the field value as one string.

normalizeValuefunc-

Normalize the filtered code before it is stored. The function should be idempotent.

onValueChangefunc-

Called with the new code and the change details. The change can be canceled.

onValueCompletefunc-

Called when the code becomes complete or a complete code is pasted, before optional form submission.

onValueInvalidfunc-

Called with the attempted input when filtering or normalization rejects characters.

readOnlybool-

Keep the field focusable while preventing edits.

renderelement
| func
-

Replaces the outer field element while preserving its styles and behavior. Accepts an element or a function that receives the root props and Base UI field state.

requiredbool-

Require a complete code before form submission.

size'medium'
| 'small'
| string
'medium'

The size of the field. Augment OTPFieldPropsSizeOverrides to add a custom size.

slotProps{ formHelperText?: func
| object, group?: func
| object, label?: func
| object, root?: func
| object }
-

Props for the Material slots and their corresponding Base UI parts.

slots{ formHelperText?: elementType, group?: elementType, label?: elementType, root?: elementType }-

Components used to render the field's slots.

sxArray<func
| object
| bool>
| func
| object
-

System styles for the outer root element.

See the `sx` page for more details.

validationType'alpha'
| 'alphanumeric'
| 'none'
| 'numeric'
'numeric'

The set of characters accepted in the code.

valuestring-

The code when controlling the field. Use an empty string for an empty field.

The ref is forwarded to the root element.

Theme default props

You can use MuiOTPField to change the default props of this component with the theme.

Slots

Slot nameClass nameDefault componentDescription
root.MuiOTPField-rootFormControlThe outer field container.
label.MuiOTPField-labelFormLabelThe visible field label.
group.MuiOTPField-group'div'The host for Base UI OTPField.Root.
formHelperText.MuiOTPField-formHelperTextFormHelperTextThe supporting or error text.

CSS classes

These class names are useful for styling with CSS. They are applied to the component's slots when specific states are triggered.

Class nameRule nameDescription
.MuiOTPField-inputinputStyles applied to each character input.
.MuiOTPField-separatorseparatorStyles applied to each decorative separator.

You can override the style of the component using one of these customization options:

Source code

If you did not find the information in this page, consider having a look at the implementation of the component for more detail.