Alternatively, you can use the errorPlacement callback function to act on a specific element that has not passed validation. For example, the code below uses the errorPlacement callback to set the class of each invalid tag of the parent element of a form element to "error", and then removes the "error" class after checking the element:
form.validate({ rules: { Name: { required: true }, Email: { required: true , regex: "^[0-9a-zA-Z.+_\-] +@ {1}[0-9a-zA-Z.+_\-]+\\.+[a-zA-Z]{2,4}$" } }, messages: { Name: { required: "Please give us your name" }, Email: { regex: "Please enter a valid email address" } }, errorPlacement: function(error, element) { element.parent().addClass("error"); }, success: function(element) { $("#" + element.attr("for")).parent().removeClass("error"); } });
source share