23

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

Community
  • 1
  • 1
Asif Hameed
  • 1,353
  • 9
  • 25
  • 46

1 Answers1

37

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; }
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Hadas
  • 10,188
  • 1
  • 22
  • 31
  • 3
    @Hadas Could you explain what you have written in the regularExpression data annotation? I am trying to build a password req – Parth Dec 02 '15 at 02:46
  • 1
    Just: [RegularExpression("^((?=.*[a-z])(?=.*[A-Z])(?=.*\\d)).+$", ErrorMessage = "Error message")] – Jose Ortega Sep 02 '16 at 07:19