Blazor TextInput
The Blazor Bootstrap TextInput component is constructed using an HTML input of type 'text'.
Basic usage #
Text alignment #
You can change the text alignment according to your need. Use the
TextAlignment
parameter to set the alignment. In the below example, alignment is set to center and end.Disable #
Use the
Disabled
parameter to disable the TextInput
.Entered text:
<div class="mb-3">
<TextInput @bind-Value="@enteredText" Disabled="@disabled" Placeholder="Enter text" />
</div>
<div class="mb-3">Entered text: @enteredText</div>
<Button Color="ButtonColor.Primary" @onclick="Enable"> Enable </Button>
<Button Color="ButtonColor.Secondary" @onclick="Disable"> Disable </Button>
<Button Color="ButtonColor.Warning" @onclick="Toggle"> Toggle </Button>
@code {
private string? enteredText = null;
private bool disabled = true;
private void Enable() => disabled = false;
private void Disable() => disabled = true;
private void Toggle() => disabled = !disabled;
}
Also, use Enable() and Disable() methods to enable and disable the
TextInput
.NOTE
Do not use both the Disabled parameter and Enable() & Disable() methods.
Entered text:
<div class="mb-3">
<TextInput @ref="textInputRef" @bind-Value="@enteredText" Placeholder="Enter text" />
</div>
<div class="mb-3">Entered text: @enteredText</div>
<Button Color="ButtonColor.Secondary" @onclick="Disable"> Disable </Button>
<Button Color="ButtonColor.Primary" @onclick="Enable"> Enable </Button>
@code {
private TextInput? textInputRef;
private string? enteredText = null;
private void Disable() => textInputRef.Disable();
private void Enable() => textInputRef.Enable();
}
Max length #
Use the
MaxLength
parameter to set the maximum length of the TextInput
.Valdations #
Like any other blazor input component,
TextInput
supports validations.
Add the DataAnnotations on the TextInput
component to validate the user input before submitting the form.
In the below example, we used Required attribute.
Events: ValueChanged #
This event fires when the
TextInput
value changes, but not on every keystroke.