Skip to content
GitHub11 stars

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

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

<div class="otp-field-field">
    <label for="@_id" class="otp-field-label">
        Verification code

Usage guidelines

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<OtpFieldRoot>
    <OtpFieldInput />
</OtpFieldRoot>

Examples

Labeling an OTP field

Use a native <label> whose for matches the first input's id. Each input has an aria-label such as "Character 2 of 6", so assistive technology announces which slot is focused. Add aria-describedby to the root when supporting text should be announced with the field.

OTP field with a native label and description
<div>
    <label for="verification-code">Verification code</label>
    <OtpFieldRoot Length="6" aria-describedby="verification-code-description">
        <OtpFieldInput id="verification-code" />
        <OtpFieldInput />
        <OtpFieldInput />
        <OtpFieldInput />
        <OtpFieldInput />
        <OtpFieldInput />
    </OtpFieldRoot>
    <p id="verification-code-description">Enter the 6-character code we sent to your device.</p>
</div>

Submitting on completion

Set AutoSubmit to submit the owning form as soon as every slot is filled, or use OnValueComplete to react to completion without submitting.

Alphanumeric verification codes

Use ValidationType="OtpValidationType.Alphanumeric" for recovery, backup or invite codes that mix letters and numbers.

Accept letters and numbers for backup codes such as A7C9XZ.

<div class="otp-field-alphanumeric-field">
    <label for="@_id" class="otp-field-alphanumeric-label">
        Recovery code

Grouped layouts

Wrap groups of inputs in your own layout elements, with a separator between them, to present the code in smaller chunks such as 123-456.

<div class="otp-field-grouped-field">
    <label for="@_id" class="otp-field-grouped-label">
        Verification code

Masked entry

Use Mask when the code should be obscured while it's typed.

Use Mask to obscure the code on shared screens.

<div class="otp-field-masked-field">
    <label for="@_id" class="otp-field-masked-label">
        Access code

API Reference

OtpFieldRoot

Renders a <div> element.

ParameterTypeDefault
Lengthrequired int 6
Value string? -
ValueChanged

Called with the new value when Value changes, for @bind-Value.

EventCallback<string> -
OnValueChange

Runs before the value changes, with the reason; call Cancel() to keep it.

EventCallback<ValueChangeEventArgs<string>> -
OnValueComplete EventCallback<string> -
AutoSubmit bool false
Mask bool false
ValidationType OtpValidationType OtpValidationType.Numeric
Disabled

Whether the component should ignore user interaction.

bool false
ReadOnly

Whether the user should be unable to change the value. It stays focusable.

bool false
Required

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

bool false
Name string? -
AutoComplete string "one-time-code"
Render RenderFragment<OtpFieldRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
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 a valid state (when wrapped in FieldRoot).

-
data-invalid

Present when the OTP field is in an invalid state (when wrapped in FieldRoot).

-
data-dirty

Present when the OTP field's value has changed (when wrapped in FieldRoot).

-
data-touched

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

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

-

OtpFieldInput

Renders a <input> element.

ParameterTypeDefault
Render RenderFragment<OtpFieldInputRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
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 a valid state (when wrapped in FieldRoot).

-
data-invalid

Present when the OTP field is in an invalid state (when wrapped in FieldRoot).

-
data-dirty

Present when the OTP field's value has changed (when wrapped in FieldRoot).

-
data-touched

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

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

-