# Blazor OTP Input (Security Code)

URL: https://flexcoreui.com/demo/security-code  
Section: Components  
Controls: SecurityCodeControl  
Keywords: OTP input, one-time password, verification code, PIN input, security code

A Blazor input for verification codes, PINs and short references: one box per character, numeric or alphanumeric, with an event when filled.

## Documentation

### Params

| Name | Type | Default | Description |
|---|---|---|---|
| Value | string? | null | The joined characters. Two-way with @bind-Value; a cleared middle box leaves a space. |
| Length | int | 6 | Number of boxes. |
| Type | SecurityCodeControl.SecurityCodeType | Numeric | Numeric takes digits only and renders type="tel"; AlphaNumeric takes any character. |
| Disabled | bool | false | Disables every box and grays the host. The base Enabled is not read by this control. |
| CssClass / Style (base) | string? | null | On the host element. The base Visible is not applied. |
| Id / Title / Tag / Data / Parent (base) |  | null | Accepted but not rendered. A written id binds to Id, so it does not reach the host. |
| (unmatched attributes) | Dictionary<string,object> |  | Written on the host <div>, not on the boxes, such as data-*. |

### Events

| Name | Type | Description |
|---|---|---|
| ValueChanged | string? | After every keystroke that changes a box, with the joined characters. |
| OnComplete | string | Once every box is filled, after ValueChanged, with the full code. Fires again if a box changes while the code is complete. |

### Types

| Name | Description |
|---|---|
| Numeric | Decimal digits (the control tests char.IsDigit, so not only 0–9). Another character is left out of Value, though the box still shows it until it is replaced. |
| AlphaNumeric | Any single character per box, as typed: no case folding and no filtering. |

### Css

| Name | Description |
|---|---|
| fx-security-code-host · .disabled | Inline flex row, 0.5rem gap; disabled grays the boxes and sets cursor: not-allowed. |
| fx-security-code-cell | Each box: 42×48px, bold 1.25rem text, 2px border; focus turns the border blue with a 3px ring. |

### Keys

| Name | Description |
|---|---|
| Tab / Shift+Tab | Move to the next or previous box. |
| Character | Fills the box. In Numeric mode only digits are added to Value. |
| Backspace | Clears the box's character; focus stays in the box. |
| Paste | Puts the first character of the pasted text in the box. |

## Examples

### Verify

```razor
@using Fx.ControlKit

<div class="vc">
    <div class="vc-head"><span class="vc-icon bi bi-shield-lock" aria-hidden="true"></span><p class="vc-lead">We texted a code to Hermione Ranger's phone ending 0142. Enter it to confirm leave request LR-0107.</p></div>
    <div class="code-row">
        <SecurityCodeControl @bind-Value="_code" Length="6" Disabled="@(_accepted == true)" OnComplete="Check" />
        <ButtonControl CssClass="btn-sm" OnClick="NewCode">Send a new code</ButtonControl>
        <BadgeControl Text="@(_accepted == true ? "Confirmed" : "Awaiting code")" Variant="@(_accepted == true ? BadgeVariant.Success : BadgeVariant.Secondary)" />
    </div>
    <p class="result @(_accepted == true ? "ok" : _accepted == false ? "bad" : null)" role="status">@Status</p>
</div>
```

```csharp
// Verification code
private const string Expected = "492018";
private string? _code = "";
private bool? _accepted;
private string Status => _accepted switch
{
    true => "Code accepted. Leave request LR-0107 is confirmed, and the boxes are locked.",
    false => "That code does not match. Type it again, or send a new code.",
    _ => $"{(_code ?? "").Replace(" ", "").Length} of 6 digits entered",
};
private void Check(string code)
{
    _accepted = code == Expected;
    if (_accepted == false) _code = "";   // empty the boxes for a fresh try
}
private void NewCode()
{
    _code = "";
    _accepted = null;
}
```

### Reference

```razor
@using Fx.ControlKit

<div class="vc">
    <div class="vc-head"><span class="vc-icon bi bi-search" aria-hidden="true"></span><p class="vc-lead">Type an employee id without its hyphen: three letters and four digits, such as EMP0017, EMP0042 or EMP0051.</p></div>
    <div class="code-row">
        <SecurityCodeControl @bind-Value="_employeeId" @bind-Value:after="ResetFound" Length="7" Type="SecurityCodeControl.SecurityCodeType.AlphaNumeric" OnComplete="Find" />
        <ButtonControl CssClass="btn-sm" OnClick="ClearEmployeeId">Clear</ButtonControl>
    </div>
    <p class="result @(_found is null ? null : _found.StartsWith("No ") ? "bad" : "ok")" role="status">@(_found ?? $"{(_employeeId ?? "").Replace(" ", "").Length} of 7 characters entered")</p>
</div>
```

```csharp
// Employee id
private static readonly Dictionary<string, string> Employees = new(StringComparer.OrdinalIgnoreCase)
{
    ["EMP0017"] = "EMP-0017: Hermione Ranger, Reservations",
    ["EMP0042"] = "EMP-0042: Luna Goodlove, Tour Operations",
    ["EMP0051"] = "EMP-0051: Neville Longbutton, IT",
};
private string? _employeeId = "";
private string? _found;
private void ResetFound() => _found = null;   // an edited id no longer shows the last lookup
private void Find(string id) =>
    _found = Employees.TryGetValue(id, out var employee) ? employee : $"No employee with id {id.ToUpperInvariant()}";
private void ClearEmployeeId()
{
    _employeeId = "";
    _found = null;
}
```

