我对密码进行了比较验证 - 确认密码字段以及服务器验证以检查密码是否符合最少字符数。
看法:
@Html.PasswordFor(model => model.password)
@Html.PasswordFor(model => model.repeatPassword)
模型:
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; }
}
控制器动作方法:
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);
}
}
执行服务器验证时,警告消息会正确显示,之后比较验证将不再起作用。任何想法?