javascript - Jquery Validate resetForm does not clear error messages -


i using tabs navigate between forms , when user presses next button, form validation takes place. if there errors, show summary of errors @ top , individual errors @ each of fields. user corrects errors, , presses next button advance next tab. when press previous button, error messages not cleared.

how clear error container @ top , individual error messages @ each of form fields provided form valid when pressing next button. have tried resetform(), didn't work.

here 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 goto 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 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 error in error summary container                 $('#alert-form-' + formid).html(msg).show();                 $(alrt).html(msg).show();                   $('html, body').animate({ scrolltop: $(alrt).offset().top - 20}, 500);              }            }         }); });  

normally, resetform() should removing default label elements containing error messages.

your code:

var formid = $(this).closest('form').attr('id'); // <- id of form var form = $('#'+ formid);                       // <- form object  if (form.valid()){    formid.validate().resetform(); // <- should `form` variable, not `formid`. } 

however, formid variable represents id of form it's not proper selector. since form variable represents proper selector, $('#'+ formid), you'll need use form.validate().resetform() rather formid.validate().resetform()

see documentation: jqueryvalidation.org/validator.resetform


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -