If the jquery form validation is ok, then I want to run the function before submitting the form

I use this to validate my form, and this is normal:


    $(document).ready(function() {
        $("#myform").validate();
    });

But I need to run the function before submitting the form, but only if the verification has passed. When I try to add a function after checking, for example:


    $(document).ready(function() {
         $("#myform").validate();
         $('#submitButton').click(function() {
            ...
         });
    });

executed even if the form fails validation.

I also tried putting the submit function in the submitButton click, and the same thing happens - the function is executed even if the form fails validation:


    $(document).ready(function() {
         $('#submitButton').click(function() {
            $("#myform").validate();
            ...
            ...
         });
    });
+3
source share
1 answer

You can use callback.

$("#myform").validate({
   submitHandler: function(form) {
     // do other stuff for a valid form
     form.submit();
   }
})

Source .

+6
source

Source: https://habr.com/ru/post/1787350/


All Articles