ASP MVC验证密码和确认密码

时间:2013-10-15 07:36:21

标签: c# asp.net-mvc asp.net-mvc-3

我必须验证密码并确认密码。我使用Compare,但是当密码和确认密码为空时,显示ErrorMessage。

当密码和确认密码都为空时,该怎么办,不应显示ErrorMessage。

7 个答案:

答案 0 :(得分:7)

我推荐使用FluentValidation库,您可以在其中以声明方式指定此类复杂方案。

RuleFor(customer => customer.Password)
.Equal(customer => customer.PasswordConfirmation)
.When(customer=>!String.IsNullOrWhitespace(customer.Password));

您可以使用NuGet

安装库
PM> Install-Package FluentValidation.MVC4

https://www.nuget.org/packages/FluentValidation.MVC4/

答案 1 :(得分:4)

使用[Compare]进行相等匹配,使用[Required]来避免空值。

答案 2 :(得分:4)

使用FluentValidation,我更喜欢将验证器拆分为不同的规则。

首先只能是

 RuleFor(x => x.Password).NotEmpty().WithMessage("Please enter the password");
 RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Please enter the confirmation password");

第二,两者之间的比较:

 RuleFor(x => x).Custom((x, context) =>
        {
            if (x.Password != x.ConfirmPassword)
            {
                context.AddFailure(nameof(x.Password), "Passwords should match");
            }
        });

最后看起来可能像这样:

public class ResetPasswordForUpdateDtoValidator : AbstractValidator<ResetPasswordForUpdateDto>
{
    public ResetPasswordForUpdateDtoValidator()
    {
        CascadeMode = CascadeMode.StopOnFirstFailure;

        RuleFor(x => x.Password).NotEmpty().WithMessage("Please enter the password");
        RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Please enter the confirmation password");

        RuleFor(x => x).Custom((x, context) =>
        {
            if (x.Password != x.ConfirmPassword)
            {
                context.AddFailure(nameof(x.Password), "Passwords should match");
            }
        });
    }
}

答案 3 :(得分:0)

我不太明白你需要什么,但是:

如果需要允许空密码,请使用[Required]以及[Compare]
如果您,请仅保留[Compare]属性。

答案 4 :(得分:0)

如果密码是可选的,则仅使用[比较]属性。

答案 5 :(得分:0)

密码并确认密码验证

尝试这个:

           <script>
function checkPass()
{
    var Password = document.getElementById('Password');
    var Confirm_Password = document.getElementById('Confirm_Password');
   var message = document.getElementById('confirmMessage');
    var goodColor = "#66cc66";
    var badColor = "#ff6666";
    if(Password.value == Confirm_Password.value){
       Confirm_Password.style.backgroundColor = goodColor;
        message.style.color = goodColor;
        message.innerHTML = "Passwords Match!"
    }else{
       Confirm_Password.style.backgroundColor = badColor;
        message.style.color = badColor;
        message.innerHTML = "Passwords Do Not Match!"
    }
} 
</script>


<div> <link rel="stylesheet" type="text/css" href="/code_examples/tutorial.css">
<script type="text/javascript" src="/code_examples/passtest.js"></script>
 <div class="tutorialWrapper"></div>


<tr>   <td>  Password : </td> <td>   <div class="fieldWrapper">

            <input type="password" name="Password" id="Password">
        </div> </td>  </tr>   

                 <tr>   <td>  

                        Confirm Password : </td> <td> 
        <div class="fieldWrapper">

            <input type="password" name="Confirm_Password" id="Confirm_Password" onkeyup="checkPass(); return false;">
            <span id="confirmMessage" class="confirmMessage"></span></td></tr>

答案 6 :(得分:0)

请记住,某些验证器仅在服务器端起作用,例如“何时”和“必须”

要在客户端上正确显示错误消息,您只需执行以下操作:

RuleFor(x => x.ConfirmPassword).NotEmpty().Equal(x => x.Password);

客户端(来自docs)支持以下内容:

  • NotNull / NotEmpty
  • 匹配项(正则表达式)
  • InclusiveBetween(范围)
  • 信用卡
  • 电子邮件
  • EqualTo(跨属性平等比较)
  • MaxLength
  • MinLength
  • 长度
相关问题