The problem is as follows,
The regular expression validation message on my view is being shown when another validation message is present, this happens even when the string matches the expression and seems like it is being triggered by other validation messages.
View
[Required]
[DataType(DataType.Password)]
[RegularExpression(@"^(?=.{8})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)", ErrorMessage = "message")]
[Display(Name = "New Password")]
public string NewPassword { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("NewPassword")]
[Display(Name = "Confirmation Password")]
public string NewPasswordConfim { get; set; }
In the above model I have my regular expression that should enforce a password with 1 lowercase, 1 uppercase and 1 numeric character, the other validation is comparing the password confirmation with the new password field.
The reg expression / val message works when the string is valid or the confirmation field matches the new password field but if the confirmation field does not match then both validation messages display.
Controler
if (Regex.IsMatch(model.NewPassword, @"^(?=.{8})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)"))
{
if (model.NewPassword != model.NewPasswordConfim)
{
return View("PasswordResetVerification", model);
}
}
In my controler I added a check to make sure that the reg expression is valid (as a test) and this check has shown me in cases where the string is a match to the expression but the confirmation password field does not match the new password field the reg expression error message is still displayed in error.
View
<div class="form-group">
<label class="col-md-2 control-label" for="NewPassword">New Password</label>
<div class="col-md-4">
<input type="password" class="form-control" id="NewPassword" name="NewPassword" placeholder="Password" value="@Model.NewPassword" />
@Html.ValidationMessageFor(m => m.NewPassword)
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="NewPasswordConfim">Confim New Password</label>
<div class="col-md-4">
<input type="password" class="form-control" id="NewPasswordConfim" name="NewPasswordConfim" placeholder="Confim Password" value="@Model.NewPasswordConfim" />
@Html.ValidationMessageFor(m => m.NewPasswordConfim)
</div>
</div>
