I have a compare validation for a password - confirm password fields and also a server validation to check if the password fits with a minimum number of characters.
View:
@Html.PasswordFor(model => model.password)
@Html.PasswordFor(model => model.repeatPassword)
Model:
public class Model_Clerk
{
public int clerkID { get; set; }
public string password { get; set; }
[Compare("password", ErrorMessage = "Error comparing password and password confirm values")]
public string repeatPassword { get; set; }
}
Controller action method:
public ActionResult SaveClerk(Model_Clerk model)
{
//Password minimum lenght
if (!string.IsNullOrEmpty(model.password) && model.password.Trim().Length < 5)
{
ModelState.AddModelError(model.password, "Password must be at least 5 characters long");
}
if (ModelState.IsValid)
{
//Save logic here...
}
else
{
return PartialView("EditClerks", model);
}
}
When the server validation is executed the warning message appears correctly, and after that the compare validation will not work anymore. Any idea?