I am trying to use the JQuery Validation Plugin to do a client side form validation.
I build my form using css bootstrap. So I wan to use the help-block
class to add has-error
class to highlight the error. and where there is no error, I don't want to add any classes.
Here is what I have done
$('#myForm').validate({
errorElement: "em",
errorPlacement: function (error, element) {
// Add the `help-block` class to the error element
error.addClass("help-block");
if (element.prop("type") === "checkbox") {
error.insertAfter(element.parent("label"));
} else {
error.insertAfter(element);
}
},
highlight: function (element, errorClass, validClass) {
$(element).parents("div").addClass("has-error");
},
unhighlight: function (element, errorClass, validClass) {
$(element).parents("div").removeClass("has-error");
}
});
the problem with the above code is that when an error takes place all the field "valid or invalid" will get colored red. even if one field is not required it still gets highlighted.
How can I avoid adding the "red" color on the valid fields?