javascript - JQuery submits the form on second or third click - Validate -


i can't figure out why validation not working on first click later.

i've add validate plugin js file form being validated after click.

as can see, validator set when document ready. then, when form clicked, validator checks, whether form valid.

function submit(form) {     var formdata = new formdata($(form).get(0));     alert('thank message! our support reply possible.');     $.ajax({         url: '/contact-us-ajax/',         type: 'post',         data: formdata,         async: true,         cache: false,         contenttype: false,         processdata: false,         success: function (data) {             alert('success');         }     });      return false; }  $(document).ready(function () {     jquery.validator.setdefaults({         debug: true,         success: "valid"     });     $("#contact-us-form").submit(function (b) {         b.preventdefault();         this_form = $(this);          $("#contact-us-form").validate({             rules: {                 sender: {                     required: true                 },                 subject: {                     required: true                 },                 message: {                     required: true                 }             },             messages: {                 sender: {                     required: "short description can't empty. please fill field."                 },                 subject: {                     required: "please choose current language."                 },                 message: {                     require_from_group: "either fill form text or attach file (below)."                 }             }, submithandler: function (this_form) {                 submit(this_form);             }         });     }); }); 

i've figure out problem was. after first click, added validator , on second click, form validated , submitted.

here working code:

function submit(form) {     var formdata = new formdata($(form).get(0));     alert('thank message! our support reply possible.');     $.ajax({         url: '/contact-us-ajax/',         type: 'post',         data: formdata,         async: true,         cache: false,         contenttype: false,         processdata: false,         success: function (data) {             alert('success');         }     });     return false; }  function attachvalidate() {         $("#contact-us-form").validate({             rules: {                 sender: {                     required: true                 },                 subject: {                     required: true                 },                 message: {                     required: true                 }             },             messages: {                 sender: {                     required: "short description can't empty. please fill field."                 },                 subject: {                     required: "please choose current language."                 },                 message: {                     require_from_group: "either fill form text or attach file (below)."                 }             }, submithandler: function (this_form) {                 submit(this_form);             }         }); }  $(document).ready(function () {     jquery.validator.setdefaults({         debug: true,         success: "valid"     });     attachvalidate();     $("#contact-us-form").submit(function (b) {         b.preventdefault();         $(this).validate();         });  }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -