PinField
Since 0.1.0-alpha.21A pin field accepts a short code, with a box for each character, such as a one-time code from an authenticator app, a text message or an email, or a PIN. The boxes sit under a single text input, so pasting a whole code, a phone's autofill and screen readers work as in any text field. To show the QR code that sets up an authenticator app, see QrCode.
A one-time code
A pin field binds to a string and accepts six digits by default. Phones offer a code that just arrived by text message or email (autocomplete="one-time-code"), and show the number keyboard.
Static
Preview
Code
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="pin-field-usage" OnValidSubmit="Check" MaxWidth="24.Rem" Grow>
<PinField @bind-Value="Model!.Code" Hint="We sent a code to [email protected]. This example takes 123456." />
<Row><Button Submit>Verify</Button></Row>
if (_verified is { } verified)
{
<Alert Status="(verified ? Status.Success : Status.Danger)">(verified ? "Verified." : "That code didn't work. Check the latest email.")</Alert>
}
</Form>
{
[SupplyParameterFromForm(FormName = "pin-field-usage")]
private EmailCheck? Model { get; set; }
private bool? _verified;
protected override void OnInitialized() => Model ??= new();
// Spaces and dashes are gone by now: "123 456" arrives as "123456".
private void Check() => _verified = Model!.Code == "123456";
public sealed class EmailCheck
{
[Required, Display(Name = "Verification code")]
public string? Code { get; set; }
}
}Static: Verify posts the code without a full reload.
Add this example to a page with the izzy CLI:
izzy add component PinField --at <file>:<line>Spaces and dashes are dropped, so a code pasted as "123 456" or "123-456" becomes 123456. The form doesn't validate until the code has every digit, on the server too, and the message says how many: "The Verification code field must be 6 digits." Check that the code is right in OnValidSubmit. Before trusting a posted value, see Values a form post can change.
The form doesn't submit when the last digit is entered, since a user who pastes a code or corrects a digit may not be done. Add a button that submits it.
PINs and codes with letters
To hide a PIN, set Secret. The boxes show dots, and the input becomes a password input, so screen readers don't read it out and a static page doesn't fill it in again after a form post. Set AutoComplete to current-password or new-password, so password managers save it and phones don't offer a texted code.
For a code with letters, such as a pairing code, set Letters. Letters are upper-cased as they're typed, and phones show the full keyboard.
Static
Preview
Letters and digits, as shown on the device.
Code
@* A PIN: four digits, masked, saved by password managers rather than offered from a text message. *@
<Stack Gap="4">
<PinField @bind-Value="_pin" Label="PIN" Length="4" Secret AutoComplete="current-password" />
<PinField @bind-Value="_letters" Label="Pairing code" Length="5" Letters Hint="Letters and digits, as shown on the device." />
</Stack>
{
private string? _pin;
private string? _letters;
}Length sets how many boxes there are, from 2 to 32; on a narrow screen they shrink to fit.
Labels and localization
The message for a code that's too short comes from the app's text. To set it for the whole app, see Text and other languages.
| IzzyText | Sets | Default |
|---|---|---|
PinDigits | The message for a code that isn't all its digits; {0} is the label, {1} the length | "The {0} field must be {1} digits." |
PinCharacters | The same with Letters | "The {0} field must be {1} letters or digits." |
Mode support
| Feature | Static | Interactive |
|---|---|---|
| Pasting, autofill and screen readers work as in a text field | Yes | Yes |
| A box per character | Yes | Yes |
| Spaces and dashes dropped, the length checked on the server | Yes | Yes |
Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.
Without JavaScript
Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, it's one text field with wide letter spacing, not boxes; paste and autofill work the same.
Accessibility
- It's one text input named by its label, so screen readers hear one field, "Code from the app, edit text", not a box per digit; the boxes are hidden from them.
autocomplete="one-time-code"(AutoComplete) lets phones offer a code from a text message or email; phones show the number keyboard for digits.- Secret, it's a password input: screen readers don't read the characters, and the browser doesn't keep them in its form history.
- The focus ring is drawn on the box the next character goes into.
- An incomplete code gets "The Code field must be 6 digits." (
IzzyText.PinDigits, orPinCharacterswithLetters), on the server too. - With JavaScript off, it's the same input without boxes, with wide letter spacing.
Parameters
| Prop | Type | Default |
|---|---|---|
LengthHow many characters the code has. | int | 6 |
LettersLetters as well as digits, upper-cased. | bool | false |
SecretHides the characters, for a PIN: it's a password input, so screen readers don't read it, and a static page doesn't fill it in again after a form post. | bool | false |
AutoCompleteThe browser autofill hint. | string? | computed |
ReadOnlyMakes the field read-only: it can be focused, and it still posts. | bool | false |
@bind-ValueThe value, bound with @bind-Value. | string? | |
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed. | string? |
Plus 28 field props.
Full APISee also
- TextField: lets the user enter a line of text, with a label, add-ons and masks.
- QrCode: shows text, such as a link, as a QR code to scan.