TextField
An interactive page: C# runs over a Blazor Server connection.
Usage
@* Label, type and limits from the model's attributes. *@
<TextField @bind-Value="Model.Email" />
@* Your own label and a hint. *@
<TextField @bind-Value="Model.Dept" Label="Department" Hint="On your profile." />
@* Text attached to the field. *@
<TextField @bind-Value="Model.Website">
<StartAddon>https://</StartAddon>
</TextField>Labels and hints
Use Label to replace the label from the model (its [Display(Name)], or the property name in words), and Hint for help under the field. [Required] marks it.
Preview
Code
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="labels" MaxWidth="24.Rem" Grow>
<TextField @bind-Value="Model!.FullName" AutoComplete="name" />
<TextField @bind-Value="Model!.Email" Hint="We'll send the invitation here." />
<TextField @bind-Value="Model!.Dept" Label="Department" Placeholder="Design" />
</Form>
{
[SupplyParameterFromForm(FormName = "labels")]
private Invite? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public sealed class Invite
{
[Required]
public string? FullName { get; set; }
[Required, EmailAddress, Display(Name = "Work email")]
public string? Email { get; set; }
public string? Dept { get; set; }
}
}Types
Use Type for email, phone, URL, search or password. It defaults from [EmailAddress], [Phone], [Url] and [DataType(DataType.Password)], and sets the on-screen keyboard and, for email and URL, the browser's check.
@* [Phone] on the property: type="tel". *@
<TextField @bind-Value="Model.Phone" />
<TextField @bind-Value="Model.Query" Type="TextFieldType.Search" />Passwords
Use Reveal="false" to leave out the show/hide button a password field gets once IzzyBlazor's script runs.
Preview
Code
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="password" MaxWidth="24.Rem" Grow>
<TextField @bind-Value="Model!.Password" AutoComplete="new-password" Hint="At least 12 characters." />
</Form>
{
[SupplyParameterFromForm(FormName = "password")]
private Account? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public sealed class Account
{
[Required, MinLength(12), DataType(DataType.Password)]
public string? Password { get; set; }
}
}Icons
Use StartIcon or EndIcon for an icon inside the field.
Preview
Code
<Form Model="Model" FormName="icons" MaxWidth="24.Rem" Grow>
<TextField @bind-Value="Model!.Query" Type="TextFieldType.Search" StartIcon="Lucide.Search" Label="Search" HideLabel Placeholder="Search projects" />
<TextField @bind-Value="Model!.Handle" StartIcon="Lucide.AtSign" Label="Username" />
<TextField @bind-Value="Model!.Domain" EndIcon="Lucide.Globe" Label="Domain" />
</Form>
{
[SupplyParameterFromForm(FormName = "icons")]
private Profile? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public sealed class Profile
{
public string? Query { get; set; }
public string? Handle { get; set; } = "izzy";
public string? Domain { get; set; } = "example.com";
}
}Add-ons
Use StartAddon and EndAddon to attach text, a Select or a button to the field. A submit button there posts the form, so a search box works with JavaScript off.
Preview
Code
<Form Model="Model" FormName="addons" MaxWidth="26.Rem" Grow>
<TextField @bind-Value="Model!.Website">
<StartAddon>https://</StartAddon>
</TextField>
<TextField @bind-Value="Model!.Price" Label="Price">
<EndAddon><Select @bind-Value="Model!.Currency" /></EndAddon>
</TextField>
<TextField @bind-Value="Model!.Query" Type="TextFieldType.Search" Label="Search" HideLabel Placeholder="Search orders">
<EndAddon><Button Submit>Search</Button></EndAddon>
</TextField>
</Form>
{
[SupplyParameterFromForm(FormName = "addons")]
private Listing? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public enum Currency { USD, EUR, GBP }
public sealed class Listing
{
public string? Website { get; set; } = "example.com";
public string? Price { get; set; } = "24.00";
public Currency Currency { get; set; }
public string? Query { get; set; }
}
}Validation
Use the model's attributes for the rules. The browser checks required, length, range, email and URL before posting, with your ErrorMessage; the server checks every rule and shows its messages under the field.
Preview
Code
System.ComponentModel.DataAnnotations
<Form Model="Model" FormName="signup" OnValidSubmit="Save" MaxWidth="24.Rem" Grow>
<TextField @bind-Value="Model!.Username" AutoComplete="username" />
<TextField @bind-Value="Model!.Email" AutoComplete="email" />
<Row><Button Submit>Create account</Button></Row>
if (_saved)
{
<Alert Status="Status.Success">Welcome, Model!.Username.</Alert>
}
</Form>
{
[SupplyParameterFromForm(FormName = "signup")]
private Signup? Model { get; set; }
private bool _saved;
protected override void OnInitialized() => Model ??= new();
private void Save() => _saved = true;
public sealed class Signup
{
[Required(ErrorMessage = "Choose a username")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "Use 3 to 20 characters")]
public string? Username { get; set; }
[Required(ErrorMessage = "Enter your email"), EmailAddress(ErrorMessage = "Enter an email like name@example.com")]
public string? Email { get; set; }
}
}Live: Each field is checked when it changes, and submitting runs C# without a page load.
Sizes, read-only and disabled
Use Size to line up with buttons and other fields, ReadOnly for a value that posts but can't change, and Disabled for one that doesn't post.
Preview
Code
<Form Model="Model" FormName="states" MaxWidth="24.Rem" Grow>
<TextField @bind-Value="Model!.Small" Label="Small" Size="ControlSize.Sm" />
<TextField @bind-Value="Model!.Large" Label="Large" Size="ControlSize.Lg" />
<TextField @bind-Value="Model!.Id" Label="Account ID" ReadOnly Hint="Read-only: posted, but can't be changed." />
<TextField @bind-Value="Model!.Plan" Label="Plan" Disabled Hint="Disabled: not posted." />
</Form>
{
[SupplyParameterFromForm(FormName = "states")]
private Settings? Model { get; set; }
protected override void OnInitialized() => Model ??= new();
public sealed class Settings
{
public string? Small { get; set; }
public string? Large { get; set; }
public string? Id { get; set; } = "acct_48213";
public string? Plan { get; set; } = "Team";
}
}Mode support
| Feature | Off | Static | Live |
|---|---|---|---|
| Label, hint, type and limits from the model | Yes | Yes | Yes |
| Icons and add-ons | Yes | Yes | Yes |
| Checks before posting, server messages under the field | Yes | Yes | Yes |
| Show/hide button on passwords | No | Yes | Yes |
Off: JavaScript disabled. Static: a static SSR page with IzzyBlazor's script. Live: an interactive page (Server, WebAssembly or Auto).
Accessibility
- It renders a native
inputwith alabellinked to it, so clicking the label focuses the field. - The hint and, when the field is invalid, its errors are linked with
aria-describedby; an invalid field setsaria-invalid. - A field with no visible label, such as a search box in a toolbar, needs
HideLabel, not an empty label: screen readers still hear the label. - A placeholder isn't a label: it's gone once there's text, and screen readers may not read it.
- Add-on text isn't part of the field's name, so put a unit that matters in the label too, as in "Price (USD)".
- The show/hide button is a toggle named "Show password" (
ShowPasswordLabel), pressed while the password shows.
Parameters
| Prop | Type | Default |
|---|---|---|
ReadOnlyMakes the field read-only: it can be focused and its text selected, and it still posts. | bool | false |
TypeThe kind of text. | TextFieldType? | |
PlaceholderExample text shown when empty. | string? | |
AutoCompleteThe browser autofill hint, such as email, name, new-password. | string? | |
MaxLengthMaximum length. | int? | computed |
MinLengthMinimum length. | int? | computed |
StartIconAn icon inside the field, before the text. | IconData? | |
EndIconAn icon inside the field, after the text. | IconData? | |
StartAddonContent attached before the control, outside its border: short text (https://, $), a button, or another field such as a Select<T> (its label is kept for screen readers but not shown). | RenderFragment? | |
EndAddonContent attached after the control, outside its border. | RenderFragment? | |
RevealFor passwords: a button that shows or hides the text. | bool | true |
ShowPasswordLabelThe reveal button's name; it's a toggle, pressed while the password shows. | string? | "Show password" |
@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 26 field props.
Full APISee also
- Form: posts on static pages and validates on interactive ones.
- TextArea: several lines, growing and counting.
- NumberField: numbers, with stepper buttons.
- FieldFrame: the same label, hint and errors around a control of your own.
- Style props:
Width,Growand margins on the whole field. - Troubleshooting: The password is empty after a failed post.