Reputation: 39
I must Validation Password and confirm password. I used Compare
, but when password and confirm password there are null, display ErrorMessage.
What to do, it should not display ErrorMessage, when Password and confirm password both are blank.
Upvotes: 2
Views: 14831
Reputation: 973
Keep in mind some validators only work on the serverside such as 'When' and 'Must'
To properly display the error message on the client you can simply do this:
RuleFor(x => x.ConfirmPassword).NotEmpty().Equal(x => x.Password);
The following are supported on the client (from the docs):
Upvotes: 1
Reputation: 1
Password and confirm password validation
try thisone:
<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>
Upvotes: 0
Reputation: 4119
Using FluentValidation, I prefer to split the validator into different rules.
First can be only
RuleFor(x => x.Password).NotEmpty().WithMessage("Please enter the password");
RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Please enter the confirmation password");
And second the comparison between both:
RuleFor(x => x).Custom((x, context) =>
{
if (x.Password != x.ConfirmPassword)
{
context.AddFailure(nameof(x.Password), "Passwords should match");
}
});
Finally it could look like this:
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");
}
});
}
}
Upvotes: 4
Reputation: 5617
I recommend the FluentValidation library where you can specify such complex scenarios declaratively.
RuleFor(customer => customer.Password)
.Equal(customer => customer.PasswordConfirmation)
.When(customer=>!String.IsNullOrWhitespace(customer.Password));
You can install the library using NuGet
PM> Install-Package FluentValidation.MVC4
https://www.nuget.org/packages/FluentValidation.MVC4/
Upvotes: 10
Reputation: 9780
I did not quite understand what do you need, but:
If you need to allow empty password, use [Required]
as well as [Compare]
.
If you don't, leave only [Compare]
attribute.
Upvotes: 0
Reputation: 1022
Use [Compare]
for the equality match and [Required]
to avoid blank values.
Upvotes: 4