Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password DataAnnotation in ASP.NET MVC 3 [duplicate]

Tags:

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

like image 476
Asif Hameed Avatar asked Jan 02 '12 09:01

Asif Hameed


People also ask

What annotation must be used for comparing passwords?

Data Annotation to validate confirm password.

What annotation must be used for comparing passwords in MVC?

1. Required Data Annotation attribute. Both the Password and the ConfirmPassword properties has been applied with the Required Data Annotation attributes. The Required Data Annotation attribute have been specified with a property Error Message with a string value.

Why to use DataAnnotations in MVC?

We can easily add validation to our application by adding Data Annotations to our model classes. Data Annotations allow us to describe the rules we want applied to our model properties, and ASP.NET MVC will take care of enforcing them and displaying appropriate messages to our users.

What is my MVC password and confirm password?

When creating users in MVC application want users to enter strong password and re-enter password to confirm. Add DataAnnotations namespace to login class. DataAnnotations have Compare attribute. [Compare("Password", ErrorMessage = "Confirm password doesn't match, Type again !")]


1 Answers

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; } 
like image 79
Hadas Avatar answered Nov 03 '22 22:11

Hadas