Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

PasswordField

Since 0.1.0-alpha.22
Lets the user enter a password, show it, and see a new one's requirements.

A password field hides what's typed as dots, with a button that shows it. It's a native password input, so it posts with the form, with JavaScript off too, and password managers fill it. Its label, hint and limits come from the model, as on a TextField.

Show and hide

Once IzzyBlazor's script runs, the field gets a button that shows and hides the password. To leave the button out, set Reveal="false". While the user types with Caps Lock on, "Caps Lock is on." shows under the field.

Static

Preview

At least 12 characters.

Code

Usage.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="password" MaxWidth="24.Rem" Grow>
    <PasswordField @bind-Value="Model!.Password" AutoComplete="new-password" Hint="At least 12 characters." />
</Form>

@code {
    [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; }
    }
}

Static: IzzyBlazor's script adds the show/hide button.

Add this example to a page with the izzy CLI:

bash
izzy add component PasswordField --at <file>:<line>

A new password's requirements

A field for a new password can list the rules a password must meet, and check off each one as the user types. Set Requirements to the rules your server checks.

Static

Preview

  • At least 15 characters

Code

NewPassword.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="password-field-new-password" OnValidSubmit="Create" MaxWidth="24.Rem" Grow>
    <TextField @bind-Value="Model!.Email" AutoComplete="username" />
    <PasswordField @bind-Value="Model!.Password" Requirements="Rules" />
    <Row><Button Submit>Create account</Button></Row>
    @if (_created)
    {
        <Alert Status="Status.Success">Account created.</Alert>
    }
</Form>

@code {
    [SupplyParameterFromForm(FormName = "password-field-new-password")]
    private SignUp? Model { get; set; }

    private bool _created;

    // The rules the server's sign-up checks: here length only, as NIST advises. With ASP.NET Core
    // Identity, copy each of its options (its defaults differ from these):
    // new PasswordRules { RequiredLength = options.RequiredLength, RequireDigit = options.RequireDigit, ... }
    private static readonly PasswordRules Rules = new() { RequiredLength = 15 };

    protected override void OnInitialized() => Model ??= new() { Email = "[email protected]" };

    private void Create() => _created = true;

    public sealed class SignUp
    {
        [Required, EmailAddress]
        public string? Email { get; set; }

        [Required, DataType(DataType.Password), Display(Name = "New password")]
        public string? Password { get; set; }
    }
}

Static: The rules are checked off as you type; Create account posts without a full reload.

The browser doesn't post the form until the password meets all of them, and the server checks them again, so a form post with JavaScript off gets the same check. AutoComplete becomes new-password, and Safari suggests passwords that meet the rules.

PasswordRules uses the names of ASP.NET Core Identity's PasswordOptions, and counts characters as Identity does: RequiredLength, RequireDigit, RequireLowercase, RequireUppercase, RequireNonAlphanumeric and RequiredUniqueChars. Its rules are off until set, where Identity's defaults are on, so copy each of Identity's values across for the list and the server to agree.

NIST SP 800-63B asks for a length of at least 15 characters, or 8 where a second factor is also needed, and says not to impose other composition rules, such as "a number". The other rules are here because many existing apps have them.

NIST also asks for a check against commonly used and breached passwords. Your server makes that check when it saves the password, such as with Pwned Passwords.

After a failed post

On a static page, the field is empty after a post that fails: the page doesn't write the password back into its HTML, so the user types it again. On an interactive page, nothing reloads and it stays.

Static

Preview

This example turns every password down.

Code

SignIn.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="sign-in" OnValidSubmit="TrySignIn" MaxWidth="24.Rem" Grow>
    @if (_failed)
    {
        <Alert Status="Status.Danger">That email and password don't match. Enter your password again.</Alert>
    }
    <TextField @bind-Value="Model!.Email" AutoComplete="username" />
    <PasswordField @bind-Value="Model!.Password" AutoComplete="current-password" Hint="This example turns every password down." />
    <Button Submit>Sign in</Button>
</Form>

@code {
    [SupplyParameterFromForm(FormName = "sign-in")]
    private Credentials? Model { get; set; }

    private bool _failed;

    protected override void OnInitialized() => Model ??= new() { Email = "[email protected]" };

    // A real app checks the password here; this one always fails, to show what comes back.
    private void TrySignIn() => _failed = true;

    public sealed class Credentials
    {
        [Required, EmailAddress]
        public string? Email { get; set; }

        [Required, DataType(DataType.Password)]
        public string? Password { get; set; }
    }
}

Static: After the failed post, the email comes back and the password field is empty.

Labels and localization

The show and hide button has a name for screen readers. To set it for the whole app, see Text and other languages.

Labels and localization
ParameterSetsDefault
ShowPasswordLabelThe name of the button that shows and hides a password"Show password"

The Caps Lock warning and the requirements come from the app's text:

Labels set in IzzyText
IzzyTextSetsDefault
CapsLockOnThe warning under the field"Caps Lock is on."
PasswordLength, PasswordUniqueThe length and different-characters rules; {0} is the number"At least {0} characters", "At least {0} different characters"
PasswordLowercase, PasswordUppercase, PasswordDigit, PasswordSymbolThe other rules"A lowercase letter", "An uppercase letter", "A number", "A symbol, such as ! or #"
RuleMet, RuleNotMetWhat screen readers hear after each rule"met", "not yet"
PasswordRulesMetAnnounced once all the rules are met"All requirements met."
PasswordRulesUnmetThe error for a password that misses one; {0} is the label"The {0} field doesn't meet every requirement."

Mode support

What works in each mode, PasswordField
FeatureStaticInteractive
Label, hint and limits from the modelYesYes
Show/hide buttonYesYes
Caps Lock warningYesYes
A new password's requirements, checked off as typed and checked on the serverYesYes

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, there's no show/hide button and no Caps Lock warning, and Requirements are checked when the form posts.

Accessibility

  • It renders a native input type="password" with a label linked to it.
  • The hint and, when the field is invalid, its errors are linked with aria-describedby; an invalid field sets aria-invalid.
  • The show/hide button is a toggle named "Show password" (ShowPasswordLabel), pressed while the password shows.
  • With script, it says "Caps Lock is on." (IzzyText.CapsLockOn) under it while typing with Caps Lock on, announced once through a polite status.
  • Requirements lists what a new password needs under the field, linked as its description; with script each item says "met" or "not yet" in its text, not by its icon alone, and "All requirements met." (IzzyText.PasswordRulesMet) is announced once when the last one is.

Parameters

PasswordField parameters
PropTypeDefault
RevealA button that shows or hides the text.booltrue
ShowPasswordLabelThe reveal button's name; it's a toggle, pressed while the password shows.string?"Show password"
RequirementsWhat a new password must have, listed under the field and checked off as it's typed; the form doesn't validate until the password meets them, on the server too.PasswordRules?
ClearableA button in the field that empties it, there while it holds something.boolfalse
ReadOnlyMakes the field read-only: it can be focused and its text selected, and it still posts.boolfalse
PlaceholderExample text shown when empty.string?
AutoCompleteThe browser autofill hint, such as email, name, current-password.string?
MaxLengthMaximum length.int?computed
MinLengthMinimum length.int?computed
StartIconAn icon inside the field, before 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?
@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?

See also

Connection lost

Rejoining the server…