Skip to content
GitHub11 stars

Accessible one-time password component with copy-paste functionality.

1
2
3
4
5
6
<InputOtp aria-label="One-time password" MaxLength="6" DefaultValue="123456">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />

About

Input OTP is a port of input-otp by @guilherme_rodz.

Installation

dnx Blazix.Cli add input-otp

Usage

<InputOtp MaxLength="6">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />
        <InputOtpSlot Index="1" />
        <InputOtpSlot Index="2" />
    </InputOtpGroup>
    <InputOtpSeparator />
    <InputOtpGroup>
        <InputOtpSlot Index="3" />
        <InputOtpSlot Index="4" />
        <InputOtpSlot Index="5" />
    </InputOtpGroup>
</InputOtp>

Composition

Use the following composition to build an InputOtp:

InputOtp
├── InputOtpGroup
│   ├── InputOtpSlot
│   ├── InputOtpSlot
│   └── InputOtpSlot
├── InputOtpSeparator
├── InputOtpGroup
│   ├── InputOtpSlot
│   ├── InputOtpSlot
│   └── InputOtpSlot
├── InputOtpSeparator
└── InputOtpGroup
    ├── InputOtpSlot
    └── InputOtpSlot

Pattern

Use the Pattern parameter to define a custom pattern for the OTP input.

<InputOtp MaxLength="6" Pattern="@InputOtpPatterns.OnlyDigitsAndChars">
    ...
</InputOtp>
<Field Class="w-fit">
    <FieldLabel for="digits-only">Digits Only</FieldLabel>
    <InputOtp id="digits-only" MaxLength="6" Pattern="@InputOtpPatterns.OnlyDigits">

Separator

Use the <InputOtpSeparator /> component to add a separator between input groups.

<InputOtp aria-label="One-time password" MaxLength="6">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />

Disabled

Use the Disabled parameter to disable the input.

1
2
3
4
5
6
<InputOtp aria-label="One-time password" id="disabled" MaxLength="6" Disabled="true" Value="123456">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />

Controlled

Use @bind-Value (or Value and ValueChanged) to control the input value.

Enter your one-time password.
<div class="space-y-2">
    <InputOtp aria-label="One-time password" MaxLength="6" @bind-Value="_value">
        <InputOtpGroup>

Invalid

Use aria-invalid on the slots to show an error state.

0
0
0
0
0
0
<InputOtp aria-label="One-time password" MaxLength="6" @bind-Value="_value">
    <InputOtpGroup>
        <InputOtpSlot Index="0" aria-invalid="true" />

Four Digits

A common pattern for PIN codes. This uses the Pattern="@InputOtpPatterns.OnlyDigits" parameter.

<InputOtp aria-label="One-time password" MaxLength="4" Pattern="@InputOtpPatterns.OnlyDigits">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />
        <InputOtpSlot Index="1" />
        <InputOtpSlot Index="2" />
        <InputOtpSlot Index="3" />
    </InputOtpGroup>
</InputOtp>

Alphanumeric

Use InputOtpPatterns.OnlyDigitsAndChars to accept both letters and numbers.

<InputOtp aria-label="One-time password" MaxLength="6" Pattern="@InputOtpPatterns.OnlyDigitsAndChars">
    <InputOtpGroup>
        <InputOtpSlot Index="0" />

Form

Verify your login
Enter the verification code we sent to your email address: m@example.com.
Having trouble signing in? Contact support
<Card Class="mx-auto max-w-md">
    <CardHeader>
        <CardTitle>Verify your login</CardTitle>

RTL

To enable RTL support in Blazix, see the RTL configuration guide.

Translated with AI for the demo, so it may have mistakes.

1
2
3
4
5
6
<Field Class="mx-auto max-w-xs">
    <FieldLabel for="input-otp-rtl">@Texts[Language]</FieldLabel>
    <InputOtp id="input-otp-rtl" MaxLength="6" DefaultValue="123456">

API Reference

InputOtp

ParameterTypeDefault
MaxLengthrequired int 0
Pattern string? -
Value string? -
ValueChanged EventCallback<string> -
DefaultValue string? -
OnComplete EventCallback<string> -
Disabled bool false
Class string? -
ContainerClass string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

InputOtpGroup

ParameterTypeDefault
Class string? -
ChildContent RenderFragment? -

Other attributes are passed to the element it renders.

InputOtpSlot

ParameterTypeDefault
Indexrequired int 0
Class string? -

Other attributes are passed to the element it renders.

InputOtpSeparator

ParameterTypeDefault
Class string? -

Other attributes are passed to the element it renders.