Using unobtrusive jQuery validation in a .NET MVC project and seems to work fine. Now I am trying to show a green checkmark when the field is validated correctly (on the client side and / or remotely).
Here is an example field declaration:
<div class="clearfix"> @Html.LabelFor(model => model.Address1, "Street") <div class="input"> @Html.TextBoxFor(model => model.Address1, new { @class = "xlarge", @maxlength = "100", @placeholder = "eg 123 Main St" }) <span class="help-message"> @Html.ValidationMessageFor(model => model.Address1) <span class="isaok">Looks great.</span> </span> <span class="help-block">Enter the street.</span> </div> </div>
What I would like to do is add the class "active" to "span.isaok", which, in turn, has a checkmark for the background image.
I tried using the / unhighlight:
$.validator.setDefaults({ onkeyup: false, highlight: function (element, errorClass, validClass) { $(element).addClass(errorClass).removeClass(validClass); $(element.form).find("label[for=" + element.id + "]").addClass("error"); $(element).parent().find("span.isaok").removeClass("active"); }, unhighlight: function (element, errorClass, validClass) { $(element).removeClass(errorClass).addClass(validClass); $(element.form).find("label[for=" + element.id + "]").removeClass("error"); if ($(element).val().length > 0) { $(element).parent().find("span.isaok").addClass("active"); } }
});
but it shows a green checkmark for all fields, even if they are empty! (hence obviously wrong)
Then I tried to use the "success" option, but it never works.
What am I missing?
Edit: So, I found this blog post and was able to use the success function, i.e.
$(function () { var settings = $.data($('form')[0], 'validator').settings; settings.onkeyup = false; settings.onfocusout = function (element) { $(element).valid(); }; var oldErrorFunction = settings.errorPlacement; var oldSuccessFunction = settings.success; settings.errorPlacement = function (error, inputElement) { inputElement.parent().find("span.isaok").removeClass("active"); oldErrorFunction(error, inputElement); }; settings.success = function (label) { var elementId = '#' + label.attr("for"); $(elementId).parent().find("span.isaok").addClass("active"); oldSuccessFunction(label); }; });
but now, if the form is invalid, both the error message and the valid label are displayed ...

and the last one disappears as soon as I click anywhere on the page.
