I need your help to solve a problem. I am using jQuery Validation plugin to validate the forms and I want to display validation summary as well as validation messages with each invalid field. I tried the following code but i ran into 2 problems;
PROBLEM 1: Error messages from invalid fields are not showing.
PROBLEM 2: The error summary is displaying the list of all errors but without the name of the fields. I think the plugin uses the 'errorList' array which holds both the name of the element as well as its error message, but i have no idea how to use this. Ref: https://jqueryvalidation.org/validate
HTML
<form id="theForm" class="formContainer">
<div id="errorSummary">
<div class="summaryTitle">
<!-- FOR ERROR TITLE -->
</div>
<ul>
<!-- FOR ERROR LIST -->
</ul>
</div>
<div class="formbox">
<label>First Name:</label>
<input type="text" name="FirstName" class="required">
</div>
<div class="formbox">
<label>Last Name:</label>
<input type="text" name="Last Name" class="required">
</div>
<button type="Submit" id="submitButton">Submit</button>
</form>
SCRIPT
<script>
$(document).ready(function(){
$("#theForm").validate({
debug:true,
errorLabelContainer: "#errorSummary ul",
wrapper: "li",
invalidHandler: function(event, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? '<h4>Submission Failed</h4>You missed 1 field. It has been highlighted'
: '<h4>Submission Failed</h4>You missed ' + errors + ' fields. They have been highlighted';
$("#errorSummary div.summaryTitle").html(message);
$("#errorSummary").show();
} else {
$("#errorSummary").hide();
}
},
});
});
</script>
PLEASE NOTE: I am already using errorPlacement property of the plugin for other use
errorPlacementproperty of the plugin for other use" ~ since that particular function is solely responsible for placing the error messages next to each field, don't you think that may be the root cause of your missing messages, and something you should be showing us here?showErrorsinstead ofinvalidHandler. Notice thatthis.defaultShowErrors()will cause the default messages to appear next to each field; unless you've broken that by improperly over-riding theerrorPlacementcallback. See: jqueryvalidation.org/validate/#showerrors