2

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

怎么办,当密码和确认密码都为空时,它不应该显示错误消息。

4

7 回答 7

9

我推荐 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/

于 2013-10-15T08:03:05.490 回答
4

用于[Compare]相等匹配并[Required]避免空白值。

于 2013-10-15T07:39:17.270 回答
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");
            }
        });
    }
}
于 2018-08-08T19:07:49.733 回答
0

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

于 2013-10-15T08:09:35.347 回答
0

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

如果您需要允许空密码,请使用[Required]以及[Compare]
如果你不这样做,只留下[Compare]属性。

于 2013-10-15T07:40:04.877 回答
0

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

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

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

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

  • 非空/非空
  • 匹配(正则表达式)
  • InclusiveBetween(范围)
  • 信用卡
  • 电子邮件
  • EqualTo(跨属性相等比较)
  • 最长长度
  • 最小长度
  • 长度
于 2019-04-27T15:59:58.150 回答
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>
于 2018-09-17T06:33:45.113 回答