forum

Home / DeveloperSection / Forums / ASP.NET MVC 5 Validation - Property are different

ASP.NET MVC 5 Validation - Property are different

Anonymous User 2413 26-Dec-2014

Hi I thought this was a simple one.To reduce the problem for the sake of clarity I have a three properties in my view model, user name, password and confirm password.

Obviously we want to make sure the passwords match but I also want to make sure that they do not match the username.

There are lots of hacks out there for previous version of MVC but I can't believe there is not a more subtle way of dealing with this obvious comparison in MVC5

[Required]
    [DataType(DataType.Text)]
    [Display(Name = "User name")]
    public string UserName { get; set; }
 
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [NotEqualTo("UserName", ErrorMessage = "The password can not be the same as the User name.")]
    public string Password { get; set; }
 
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }


Updated on 26-Dec-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By