Reputation: 283
I've been trying to figure this out but am having no luck. I have a model with two properties: Password
and ConfirmPassword
.
If I display these as TextBoxes (Html.TextBoxFor(...)
) in the view then the validation works fine. If, however, I display these as Password fields (Html.PasswordFor(...)
) then the validation does not work until a form is submit.
Below is the code for both my view and model. Please note that the view is just a test view and I have not put a submit button on it. Model:
public class TestViewModel
{
[Required(ErrorMessage = "Password is required")]
[StringLength(40, ErrorMessageResourceType = typeof(Resources.Shared_Editor_SecurityDetails), ErrorMessageResourceName = "PasswordLength")]
public string Password { get; set; }
[Required(ErrorMessage = "Confirm is required")]
[Compare("Password", ErrorMessage = "Must be the same as Password")]
public string ConfirmPassword { get; set; }
}
View:
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
}
If anybody is able to help then that would be much appreciated.
Many thanks!
Upvotes: 1
Views: 1711
Reputation: 283
Although we didn't get to the root cause of this issue, we have come up with a workaround.
We set a function to be triggered on blur()
for each of the password fields using jquery and the field id's. In this function we perform a $(this).valid()
or $('#idOfField').valid()
.
Equally you could perform this in the keyUp()
function too.
This is forcing the validation but if anybody can come up with an actual reason as to why we may have encountered this issue I would be very intrigued to hear it.
Upvotes: 1