Password DataAnnotation in ASP.NET MVC 3

Asif Hameed picture Asif Hameed · Jan 2, 2012 · Viewed 38.9k times · Source

Possible Duplicate:
Password validation (regex?)

I am working on asp.net MVC 3 application and I have applied

    [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; }

DataAnnotation to my Password field. I want to make sure that password must be at least 6 characters, no more than 18 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit. Do I need to add regular expression or DataType.password will do all this ?

Please suggest

Answer

Hadas picture Hadas · Jan 2, 2012

You must write exactly what you want. Write this:

[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; }