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

2
  • 2
    How is this related to entity framework? Commented Jan 2, 2012 at 10:12
  • 1
    Not related to Entity Framework Commented Jan 2, 2012 at 11:21

1 Answer 1

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; }
Sign up to request clarification or add additional context in comments.

2 Comments

@Hadas Could you explain what you have written in the regularExpression data annotation? I am trying to build a password req
Just: [RegularExpression("^((?=.*[a-z])(?=.*[A-Z])(?=.*\\d)).+$", ErrorMessage = "Error message")]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.