Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Last active November 25, 2019 06:04
Show Gist options
  • Save bcnzer/9ec872a30551bfb358324c83fc9579d9 to your computer and use it in GitHub Desktop.
Save bcnzer/9ec872a30551bfb358324c83fc9579d9 to your computer and use it in GitHub Desktop.
Registration view model with a complex password requirement
public class RegisterViewModel
{
[Required]
[Display(Name = "First Name")]
[StringLength(100, ErrorMessage = "{0} must be at least {2} characters long.", MinimumLength = 2)]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[StringLength(100, ErrorMessage = "{0} must be at least {2} characters long.", MinimumLength = 2)]
public string LastName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[EmailAddress]
[Compare("Email", ErrorMessage = "The email addresses do not match.")]
[Display(Name = "Confirmation Email")]
public string EmailConfirm { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
[Display(Name = "Password")]
[RegularExpression("^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])|(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^a-zA-Z0-9])).{8,}$", ErrorMessage = "Passwords must be at least 8 characters and contain at 3 of 4 of the following: upper case (A-Z), lower case (a-z), number (0-9) and special character (e.g. !@#$%^&*)")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The passwords do not match.")]
public string ConfirmPassword { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment