This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

OTP Field

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

Examples#

Labeling an OTP field#

Give an id to the first OTPFieldInput and use a native <label> with a matching for. Let the first input use the field label: the other inputs get aria-label="Character 2 of 6" and so on, so assistive technology can announce which slot is focused.

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

Form integration#

Use Field to handle label associations and form integration.

Pass autoSubmit to submit the owning form automatically when all slots are filled, or listen to phem-otp-field:complete to react to completion without submitting.

Alphanumeric verification codes#

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

Grouped layouts#

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

Placeholder hints#

OTPFieldInput 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.

Custom normalization#

Use normalize (uppercase or lowercase) to normalize accepted values before state updates, such as converting alphanumeric codes to uppercase. It runs after validationType filtering, and the result is filtered against validationType again.

A refused character sends phem-otp-field:invalid.

Masked entry#

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

API reference#

Import the components of the family:

use function Phem\Ui\{OTPField, OTPFieldInput, OTPFieldSeparator};

OTPField#

One input for each character of a code: Base UI OTPField.Root, on the phem-otp-field controller. Put one OTPFieldInput in it for each character. With a name (or phem:bind), a hidden input keeps the full code for the form.

PropTypeDefault
defaultValue?stringnull
validationTypestring'numeric'
normalize?stringnull
maskboolfalse
autoSubmitboolfalse
disabledboolfalse
readOnlyboolfalse
requiredboolfalse
name?stringnull
value?Boundnull
children?Htmlnull
...restmixed—

OTPFieldInput#

The input of one character. In a Field, each input is a control of the field: the label names the first one.

PropTypeDefault
...restmixed—

OTPFieldSeparator#

A mark between two groups of the slots of the code, – by default.

PropTypeDefault
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/OTPField.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close