Search code examples
c#asp.netasp.net-mvc-2comparevalidator

How to Use compare validator


Please tell me how to apply compare validater for password and confirm password in ASP.NET MVC2. Please give me some good link or any sample.

Thanks


Solution

  • This sample is taken straight from the mvc2 template and the MvcMusicStore sample (on codeplex).

    This sample assumes you are using strongly typed views.

    [PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Current password")]
        public string OldPassword { get; set; }
    
        [Required]
        [ValidatePasswordLength]
        [DataType(DataType.Password)]
        [DisplayName("New password")]
        public string NewPassword { get; set; }
    
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Confirm new password")]
        public string ConfirmPassword { get; set; }
    }