Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  168] [ 3]  / answers: 1 / hits: 26767  / 10 Years ago, tue, june 3, 2014, 12:00:00

I am using tabs to navigate between forms and when the user presses the next button, form validation takes place. If there are errors, it will show the summary of errors at the top and also individual errors at each of the fields. The user corrects the errors, and presses the Next button to advance to the next tab. When the press the previous button, the error messages are not cleared.



How would I clear the error container at the top and the individual error messages at each of the form fields provided that the form is valid when pressing the next button. I have tried the resetForm(), but that didn't work.



Here is my code



<form class=wizardTab id=graph-data>        
<div class=alert alert-error id=alert-form-graph-data></div>
<div class=row required id=frmgraphdata>
<fieldset>
<legend>Select below</legend>
<div class=inputgroup id=select_graph_data>
<label for=graph-only>
<input class=required id=graph-only name=select_graph_or_data required=required type=radio value=graph-only/>Graph
</label>
<label for=data-only>
<input class=required id=data-only name=select_graph_or_data required=required type=radio value=data-only/>Data
</label>
</div>
</fieldset>
</div>
<div class=row buttons>
<input class=btnNext id=q0_btn_next type=button value=Next &gt;/>
</div>
</form>


Jquery code:



    $('#q0_btn_next').click(function (e) {

e.preventDefault();

var formID = $(this).closest('form').attr('id');
var form = $('#'+ formID);

if (form.valid()){

//code to goto the next tab

// clear error message
form.validate().resetForm();

}


});

$('.wizardTab').each(function(){
$(this).validate({

onkeyup: false,
onclick: false,
onfocusout: false,
validClass: success,
errorClass: error,
rules: {
'select_graph_or_data': {
required: true
}
// more rules for other forms
},

invalidHandler: function(form, validator) {

if (!validator.numberOfInvalids())
return;

/*$('html, body').animate({
scrollTop: $(validator.errorList[0].element).offset().top
}, 500);*/

},

errorPlacement: function (error, element) {

if (element.parents('.inputgroup').length) {
error.insertBefore(element.parents('.inputgroup'));
element.parents('.inputgroup').addClass('error');
} else {
error.insertBefore(element);
};
},

showErrors: function (errorMap, errorList, currentForm) {

errors = this.numberOfInvalids();
var formID = this.currentForm.attributes.id.nodeValue;
var alrt = $('#alert-form-'+ formID);


if (errors > 0){

this.defaultShowErrors();

var msg = '<p>Your form has errors:</p><ul>';

$.each(errorMap, function(key, value) {
msg += '<li><a href=#' + key + '-row class=error>' + value + '</a></li>';
});
msg += '</ul>';
// Show the error in the error summary container
$('#alert-form-' + formID).html(msg).show();
$(alrt).html(msg).show();


$('html, body').animate({ scrollTop: $(alrt).offset().top - 20}, 500);

}



}
});
});

More From » jquery

 Answers
41

Normally, resetForm() should be removing the default label elements containing the error messages.



Your code:



var formID = $(this).closest('form').attr('id'); // <- the ID of the form
var form = $('#'+ formID); // <- the form object

if (form.valid()){
formID.validate().resetForm(); // <- should be your `form` variable, not `formID`.
}


However, your formID variable only represents the ID of the form so it's not a proper selector. Since your form variable represents the proper selector, $('#'+ formID), you'll need to use form.validate().resetForm() rather than formID.validate().resetForm()



See documentation: jqueryvalidation.org/Validator.resetForm


[#70747] Sunday, June 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cruzs

Total Points: 710
Total Questions: 113
Total Answers: 100

Location: Nepal
Member since Sat, Jul 18, 2020
4 Years ago
cruzs questions
Thu, Nov 26, 20, 00:00, 4 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
Sun, Aug 2, 20, 00:00, 4 Years ago
;