password validation regex data annotation code example

Example 1: data annotation for passwords

[Required]
[StringLength(18, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[RegularExpression(@"^((?=.*[a-z])(?=.*[A-Z])(?=.*\d)).+$"]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

Example 2: data annotation for passwords

[Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

Tags:

Misc Example