NvFieldpassword Component - Practical Guide for Blazor Developers
The NvFieldpassword component is a Blazor wrapper around Nova's password input component, providing secure text input with show/hide functionality and seamless integration with Blazor's data binding, forms, and validation systems.
General Blazor Concepts
For general information about data binding, forms, and validation in Nova Blazor components, see the Nova Blazor Integration Guide. This guide focuses on password input-specific features and patterns.
Basic Usage
Simple Password Input
<NvFieldpassword Label="Password" Placeholder="Enter your password" />
Password Input with Description
<NvFieldpassword
Label="New Password"
Description="Password must be at least 8 characters long"
Placeholder="Choose a strong password" />
Two-Way Data Binding
The component supports Blazor's @bind-Value syntax for seamless two-way data binding:
Simple Binding
@code {
private string password = "";
}
<NvFieldpassword
@bind-Value="password"
Label="Password"
Placeholder="Enter password" />
<p>Password length: @password.Length characters</p>
Login Form with Password Fields
@code {
private string username = "";
private string password = "";
private string confirmPassword = "";
}
<NvFieldtext
@bind-Value="username"
Label="Username"
Placeholder="Enter username" />
<NvFieldpassword
@bind-Value="password"
Label="Password"
Placeholder="Enter password" />
<NvFieldpassword
@bind-Value="confirmPassword"
Label="Confirm Password"
Placeholder="Confirm password" />
<div class="password-strength">
@if (!string.IsNullOrEmpty(password))
{
<span>Password strength: @GetPasswordStrength(password)</span>
}
</div>
@code {
private string GetPasswordStrength(string pwd)
{
if (pwd.Length < 6) return "Weak";
if (pwd.Length < 10) return "Medium";
return "Strong";
}
}
Password Visibility Control
Show/Hide Password Toggle
<NvFieldpassword
@bind-Value="password"
Label="Password"
ShowPassword="showPassword"
Placeholder="Enter password" />
<label>
<input type="checkbox" @bind="showPassword" />
Show password
</label>
@code {
private string password = "";
private bool showPassword = false;
}
Hide Password Icon
<NvFieldpassword
@bind-Value="password"
Label="Password"
HidePasswordIcon="true"
Placeholder="Password (no show/hide button)" />
Event Handling
Using ValueChanged Event
<NvFieldpassword
Label="Password"
Placeholder="Type to see validation..."
ValueChanged="OnPasswordChanged" />
<div class="validation-feedback">
@foreach (var message in validationMessages)
{
<div class="message @message.Type">@message.Text</div>
}
</div>
@code {
private List<ValidationMessage> validationMessages = new();
private void OnPasswordChanged(string value)
{
validationMessages.Clear();
if (value.Length < 8)
{
validationMessages.Add(new ValidationMessage
{
Text = "Password must be at least 8 characters",
Type = "error"
});
}
if (!value.Any(char.IsUpper))
{
validationMessages.Add(new ValidationMessage
{
Text = "Password must contain at least one uppercase letter",
Type = "warning"
});
}
if (!value.Any(char.IsDigit))
{
validationMessages.Add(new ValidationMessage
{
Text = "Password must contain at least one number",
Type = "warning"
});
}
if (validationMessages.Count == 0 && value.Length >= 8)
{
validationMessages.Add(new ValidationMessage
{
Text = "Password looks good!",
Type = "success"
});
}
}
public class ValidationMessage
{
public string Text { get; set; } = "";
public string Type { get; set; } = "";
}
}
Event with Binding
@code {
private string password = "";
private string strengthText = "";
private void OnPasswordChanged(string value)
{
password = value;
UpdatePasswordStrength(value);
}
private void UpdatePasswordStrength(string pwd)
{
if (string.IsNullOrEmpty(pwd))
{
strengthText = "";
return;
}
var score = 0;
if (pwd.Length >= 8) score++;
if (pwd.Any(char.IsUpper)) score++;
if (pwd.Any(char.IsLower)) score++;
if (pwd.Any(char.IsDigit)) score++;
if (pwd.Any(ch => !char.IsLetterOrDigit(ch))) score++;
strengthText = score switch
{
0 or 1 => "Very Weak",
2 => "Weak",
3 => "Medium",
4 => "Strong",
5 => "Very Strong",
_ => ""
};
}
}
<NvFieldpassword
@bind-Value="password"
Label="Password"
ValueChanged="OnPasswordChanged"
Placeholder="Enter password" />
@if (!string.IsNullOrEmpty(strengthText))
{
<div class="password-strength @strengthText.ToLower().Replace(" ", "-")">
Password strength: @strengthText
</div>
}
Form Integration
The component integrates seamlessly with Blazor's EditForm for validation and form submission:
Basic Form Integration
<EditForm Model="loginModel" OnValidSubmit="AuthenticateUser">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="loginModel.Username"
Label="Username"
For="() => loginModel.Username" />
<NvFieldpassword
@bind-Value="loginModel.Password"
Label="Password"
For="() => loginModel.Password" />
<button type="submit">Login</button>
</EditForm>
@code {
private LoginModel loginModel = new();
private void AuthenticateUser()
{
// Process login
Console.WriteLine("User authenticated successfully!");
}
public class LoginModel
{
[Required(ErrorMessage = "Username is required")]
public string? Username { get; set; }
[Required(ErrorMessage = "Password is required")]
public string? Password { get; set; }
}
}
Validation
Required Field Validation
<EditForm Model="changePasswordModel" OnValidSubmit="ChangePassword">
<DataAnnotationsValidator />
<NvFieldpassword
@bind-Value="changePasswordModel.CurrentPassword"
Label="Current Password"
Required="true"
For="() => changePasswordModel.CurrentPassword" />
<NvFieldpassword
@bind-Value="changePasswordModel.NewPassword"
Label="New Password"
Required="true"
Minlength="8"
For="() => changePasswordModel.NewPassword" />
<NvFieldpassword
@bind-Value="changePasswordModel.ConfirmPassword"
Label="Confirm New Password"
Required="true"
For="() => changePasswordModel.ConfirmPassword" />
<button type="submit">Change Password</button>
</EditForm>
@code {
private ChangePasswordModel changePasswordModel = new();
public class ChangePasswordModel
{
[Required]
public string? CurrentPassword { get; set; }
[Required]
[MinLength(8, ErrorMessage = "Password must be at least 8 characters")]
public string? NewPassword { get; set; }
[Required]
[Compare("NewPassword", ErrorMessage = "Passwords do not match")]
public string? ConfirmPassword { get; set; }
}
}
Password Strength Validation
<EditForm Model="registrationModel" OnValidSubmit="RegisterUser">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="registrationModel.Username"
Label="Username"
For="() => registrationModel.Username" />
<NvFieldpassword
@bind-Value="registrationModel.Password"
Label="Password"
For="() => registrationModel.Password" />
<NvFieldpassword
@bind-Value="registrationModel.ConfirmPassword"
Label="Confirm Password"
For="() => registrationModel.ConfirmPassword" />
<button type="submit">Register</button>
</EditForm>
@code {
private RegistrationModel registrationModel = new();
public class RegistrationModel
{
[Required]
public string? Username { get; set; }
[Required]
[PasswordStrengthValidation]
public string? Password { get; set; }
[Required]
[Compare("Password", ErrorMessage = "Passwords do not match")]
public string? ConfirmPassword { get; set; }
}
public class PasswordStrengthValidationAttribute : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var password = value as string;
if (string.IsNullOrWhiteSpace(password))
return new ValidationResult("Password is required");
if (password.Length < 8)
return new ValidationResult("Password must be at least 8 characters long");
if (!password.Any(char.IsUpper))
return new ValidationResult("Password must contain at least one uppercase letter");
if (!password.Any(char.IsLower))
return new ValidationResult("Password must contain at least one lowercase letter");
if (!password.Any(char.IsDigit))
return new ValidationResult("Password must contain at least one number");
if (!password.Any(ch => !char.IsLetterOrDigit(ch)))
return new ValidationResult("Password must contain at least one special character");
return ValidationResult.Success;
}
}
}
Pattern Validation
<EditForm Model="secureModel" OnValidSubmit="ProcessSecureData">
<DataAnnotationsValidator />
<NvFieldpassword
@bind-Value="secureModel.Pin"
Label="PIN Code"
Pattern="[0-9]{4}"
Maxlength="4"
Description="Enter 4-digit PIN"
For="() => secureModel.Pin" />
<NvFieldpassword
@bind-Value="secureModel.AccessCode"
Label="Access Code"
Pattern="[A-Za-z0-9]{6,12}"
Minlength="6"
Maxlength="12"
Description="6-12 alphanumeric characters"
For="() => secureModel.AccessCode" />
<button type="submit">Submit</button>
</EditForm>
@code {
private SecureModel secureModel = new();
public class SecureModel
{
[Required]
[RegularExpression("[0-9]{4}", ErrorMessage = "PIN must be exactly 4 digits")]
public string? Pin { get; set; }
[Required]
[RegularExpression("[A-Za-z0-9]{6,12}", ErrorMessage = "Access code must be 6-12 alphanumeric characters")]
public string? AccessCode { get; set; }
}
}
Advanced Scenarios
Password Confirmation with Real-time Validation
<NvFieldpassword
@bind-Value="newPassword"
Label="New Password"
ValueChanged="ValidatePasswords"
Placeholder="Enter new password" />
<NvFieldpassword
@bind-Value="confirmPassword"
Label="Confirm Password"
ValueChanged="ValidatePasswords"
Placeholder="Confirm new password" />
@if (!string.IsNullOrEmpty(validationMessage))
{
<div class="validation-message @validationType">
@validationMessage
</div>
}
@code {
private string newPassword = "";
private string confirmPassword = "";
private string validationMessage = "";
private string validationType = "";
private void ValidatePasswords(string value)
{
if (string.IsNullOrEmpty(newPassword) || string.IsNullOrEmpty(confirmPassword))
{
validationMessage = "";
validationType = "";
return;
}
if (newPassword == confirmPassword)
{
validationMessage = "✓ Passwords match";
validationType = "success";
}
else
{
validationMessage = "✗ Passwords do not match";
validationType = "error";
}
}
}
Password Generator
<div class="password-generator">
<NvFieldpassword
@bind-Value="generatedPassword"
Label="Generated Password"
ShowPassword="true"
Readonly="true" />
<button @onclick="GeneratePassword">Generate New Password</button>
<button @onclick="CopyToClipboard">Copy Password</button>
</div>
<NvFieldpassword
@bind-Value="manualPassword"
Label="Or Enter Manually"
Placeholder="Enter your own password" />
@code {
private string generatedPassword = "";
private string manualPassword = "";
private void GeneratePassword()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
var random = new Random();
generatedPassword = new string(Enumerable.Repeat(chars, 12)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private async Task CopyToClipboard()
{
if (!string.IsNullOrEmpty(generatedPassword))
{
await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", generatedPassword);
}
}
[Inject]
private IJSRuntime JSRuntime { get; set; } = default!;
}
Multi-factor Authentication Setup
<EditForm Model="mfaModel" OnValidSubmit="SetupMFA">
<DataAnnotationsValidator />
<fieldset>
<legend>Set up Multi-Factor Authentication</legend>
<NvFieldpassword
@bind-Value="mfaModel.CurrentPassword"
Label="Current Password"
For="() => mfaModel.CurrentPassword" />
<NvFieldpassword
@bind-Value="mfaModel.NewPin"
Label="New 6-digit PIN"
Pattern="[0-9]{6}"
Maxlength="6"
Description="Enter a 6-digit PIN for MFA"
For="() => mfaModel.NewPin" />
<NvFieldpassword
@bind-Value="mfaModel.ConfirmPin"
Label="Confirm PIN"
Pattern="[0-9]{6}"
Maxlength="6"
For="() => mfaModel.ConfirmPin" />
</fieldset>
<button type="submit">Enable MFA</button>
</EditForm>
@code {
private MFAModel mfaModel = new();
public class MFAModel
{
[Required]
public string? CurrentPassword { get; set; }
[Required]
[RegularExpression("[0-9]{6}", ErrorMessage = "PIN must be exactly 6 digits")]
public string? NewPin { get; set; }
[Required]
[Compare("NewPin", ErrorMessage = "PINs do not match")]
public string? ConfirmPin { get; set; }
}
}
Account Recovery with Security Questions
@if (step == 1)
{
<div class="recovery-step">
<h4>Step 1: Verify Identity</h4>
<NvFieldtext
@bind-Value="username"
Label="Username"
Placeholder="Enter your username" />
<NvFieldpassword
@bind-Value="password"
Label="Current Password"
Placeholder="Enter current password" />
<button @onclick="VerifyIdentity">Verify</button>
</div>
}
else if (step == 2)
{
<div class="recovery-step">
<h4>Step 2: Reset Password</h4>
<NvFieldpassword
@bind-Value="newPassword"
Label="New Password"
Minlength="8"
Description="At least 8 characters"
ValueChanged="ValidateNewPassword" />
<NvFieldpassword
@bind-Value="confirmPassword"
Label="Confirm Password"
ValueChanged="ValidateConfirmation" />
@if (passwordsMatch && !string.IsNullOrEmpty(newPassword))
{
<div class="success">✓ Passwords match</div>
}
else if (!passwordsMatch && !string.IsNullOrEmpty(confirmPassword))
{
<div class="error">✗ Passwords do not match</div>
}
<button @onclick="ResetPassword" disabled="@(!passwordsMatch)">Reset Password</button>
</div>
}
@code {
private int step = 1;
private string username = "";
private string password = "";
private string newPassword = "";
private string confirmPassword = "";
private bool passwordsMatch = false;
private void VerifyIdentity()
{
// Verify user identity
step = 2;
}
private void ValidateNewPassword(string value)
{
newPassword = value;
ValidatePasswords();
}
private void ValidateConfirmation(string value)
{
confirmPassword = value;
ValidatePasswords();
}
private void ValidatePasswords()
{
passwordsMatch = !string.IsNullOrEmpty(newPassword) &&
!string.IsNullOrEmpty(confirmPassword) &&
newPassword == confirmPassword;
}
private void ResetPassword()
{
// Reset password logic
Console.WriteLine("Password reset successfully!");
}
}
Best Practices
- Always use HTTPS for password forms to ensure secure transmission
- Set appropriate autocomplete values for better UX (current-password, new-password)
- Provide clear password requirements in descriptions
- Implement client-side validation for immediate feedback
- Use server-side validation as the final authority
- Consider password strength indicators for user guidance
- Allow password visibility toggle for better accessibility
- Handle password confirmation properly with real-time validation
- Never store passwords in plain text on the client side
- Clear password fields after successful form submission
Common Patterns
Login Form
<EditForm Model="loginModel" OnValidSubmit="Login">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="loginModel.Email"
Label="Email"
Type="TypeEnum.email"
Autocomplete="AutocompleteEnum.email"
For="() => loginModel.Email" />
<NvFieldpassword
@bind-Value="loginModel.Password"
Label="Password"
Autocomplete="AutocompleteEnum.currentpassword"
For="() => loginModel.Password" />
<label>
<input type="checkbox" @bind="loginModel.RememberMe" />
Remember me
</label>
<button type="submit">Sign In</button>
</EditForm>
@code {
private LoginModel loginModel = new();
public class LoginModel
{
[Required]
[EmailAddress]
public string? Email { get; set; }
[Required]
public string? Password { get; set; }
public bool RememberMe { get; set; }
}
}
Registration Form
<EditForm Model="registrationModel" OnValidSubmit="Register">
<DataAnnotationsValidator />
<NvFieldtext
@bind-Value="registrationModel.Username"
Label="Username"
For="() => registrationModel.Username" />
<NvFieldtext
@bind-Value="registrationModel.Email"
Label="Email"
Type="TypeEnum.email"
Autocomplete="AutocompleteEnum.email"
For="() => registrationModel.Email" />
<NvFieldpassword
@bind-Value="registrationModel.Password"
Label="Password"
Autocomplete="AutocompleteEnum.newpassword"
For="() => registrationModel.Password" />
<NvFieldpassword
@bind-Value="registrationModel.ConfirmPassword"
Label="Confirm Password"
Autocomplete="AutocompleteEnum.newpassword"
For="() => registrationModel.ConfirmPassword" />
<button type="submit">Create Account</button>
</EditForm>
@code {
private RegistrationModel registrationModel = new();
public class RegistrationModel
{
[Required]
public string? Username { get; set; }
[Required]
[EmailAddress]
public string? Email { get; set; }
[Required]
[MinLength(8)]
public string? Password { get; set; }
[Required]
[Compare("Password")]
public string? ConfirmPassword { get; set; }
}
}
Change Password Form
<EditForm Model="changePasswordModel" OnValidSubmit="ChangePassword">
<DataAnnotationsValidator />
<NvFieldpassword
@bind-Value="changePasswordModel.CurrentPassword"
Label="Current Password"
Autocomplete="AutocompleteEnum.currentpassword"
For="() => changePasswordModel.CurrentPassword" />
<NvFieldpassword
@bind-Value="changePasswordModel.NewPassword"
Label="New Password"
Autocomplete="AutocompleteEnum.newpassword"
For="() => changePasswordModel.NewPassword" />
<NvFieldpassword
@bind-Value="changePasswordModel.ConfirmPassword"
Label="Confirm New Password"
Autocomplete="AutocompleteEnum.newpassword"
For="() => changePasswordModel.ConfirmPassword" />
<button type="submit">Change Password</button>
</EditForm>
@code {
private ChangePasswordModel changePasswordModel = new();
public class ChangePasswordModel
{
[Required]
public string? CurrentPassword { get; set; }
[Required]
[MinLength(8)]
public string? NewPassword { get; set; }
[Required]
[Compare("NewPassword")]
public string? ConfirmPassword { get; set; }
}
}
This guide covers the most common use cases and patterns for the NvFieldpassword component in Blazor applications. For more advanced scenarios or specific requirements, refer to the component's API documentation or the Storybook examples.