Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation: Checking if form is validated?

I would like to run a function if my form has completely validated according to my defined rules (Not shown below).

The below code doesn't work as I would like it to, most certainly depending on the fact that the success function is called on a per-input basis and not globally for the complete form .

Are there any workarounds to get a global success callback that runs when the complete form validates properly?

$("#myForm").validate({

    success: function(label) {

        if ($("#myForm").valid()) {
            runMyFunction();
        }

    },

    rules: {
        //...
    }

});
like image 212
Industrial Avatar asked Nov 05 '22 17:11

Industrial


1 Answers

$("#myForm").validate({

    rules: {
        //...
    },
    success: function(label) {           

    },
   submitHandler: function(e) {
          e.preventDefault();
          runMyFunction();           
    }    
 });

REF: http://docs.jquery.com/Plugins/Validation/validate#toptions

you dont have to do

if ($("#myForm").valid()) {
            runMyFunction();
        }

the success callback is called only when the from is validated just runMyFunction()

like image 104
Rafay Avatar answered Nov 15 '22 08:11

Rafay