Forms
TwMaskedInput
TwMaskedInput is a Blazor input that enforces a structured format as you type, such as a phone number or custom code. Bind the raw value and set a Mask.
Last updated
Please be aware this component is new, may have bugs and/or be subject to change. Please report any bugs at
https://github.com/twblazor/twblazor/issues.
Basic usage
Set
Mask to a string of tokens and literals, and bind the raw value with @bind-Value. A 9 accepts a digit 0-9, A a letter A-Z or a-z and * either; every other character is a literal shown between them. The mask needs at least one token, its 9, A and * cannot be escaped, and characters past the mask's length are dropped - digits and letters fill left to right. Here the mask (999) 999-9999 formats a phone number, and the bound value stays as the ten raw digits.Value:
Custom mask
Tokens can be mixed freely. A part number such as
ABC-1234 is three letters, a dash, then four digits: the mask AAA-9999. A character the position does not accept is ignored, and the mobile keyboard matches the mask when it is all digits.Value:
Placeholder character
PlaceholderChar is the character shown for each unfilled mask position, _ by default. Choose another, such as a space, to make the unfilled positions subtler.Value:
Show mask
By default (
ShowMask is true) an empty field shows the full mask so its structure is visible up front. Set ShowMask="false" to leave an empty field empty, so the mask only reveals itself as the user types.Clearable
Clearable shows the textfield's clear button, which empties the field and sets the bound value back to an empty string.Value: 5551234567
Variants and dense
Variant and Dense work exactly as they do on TwTextfield, so a masked input lines up with the other inputs.Disabled and readonly
Disabled dims the field and prevents input. ReadOnly keeps the value readable and focusable, but it cannot be changed.RAZOR
<div class="grid gap-4 sm:grid-cols-2">
<TwMaskedInput Label="Disabled" Mask="(999) 999-9999" Value="5551234567" Disabled />
<TwMaskedInput Label="Readonly" Mask="(999) 999-9999" Value="5551234567" ReadOnly />
</div>Validation
Validation works as it does on
TwTextfield: pass a Validator that returns an error message, or null when the value is valid, or set Invalid and ErrorMessage yourself. The message is tied to the field with aria-describedby, so screen readers read it with the field. The mask lives in the field's value, so screen readers announce the placeholder characters and Required alone does not block submit - use model validation through ValueExpression for a required field.Update on input
Typed text is committed when the field loses focus or Enter is pressed. Set
BindEvent="oninput" to update the value on every keystroke instead.Value: