Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery If myform.valid() is true then fire another jQuery function?

I have a jQuery function that i would like to execute only if the form validation is true
I have the validation and other jQuery working fine.
the problem is now both validation and other function are executing same time.
I would like to run the other function only if the form validation is true. here is my script

<script>
            $(document).ready(function () {
                $.validator.addClassRules({
                    fileName: {
                        required: true
                    }
                });
                $("#myform").validate();
                if ($("#myform").valid()) {
                    $(function () {
                        $('#myform').submit(function () {
                            var cboxes = ($('input:checkbox:checked').filter(":checked").length);
                            var nboxes = ($(":checkbox:not(:checked)").length);
                            if ((cboxes > 0) && (nboxes > 0)) {
                                confirm('You have Checked only few locations among the List. \n Are you sure, you do not want to Prescribe from the other locations? ');
                            } else if (cboxes == 0) {
                                alert('Please select atleast One Address \n Where you would prefer to prescribe from.');
                                return false;
                            }
                            else {
                                return true;
                            }
                        });
                    });
                }
            });
</script>

please advise how can i get this done?

like image 753
HaBo Avatar asked Oct 06 '11 13:10

HaBo


1 Answers

If you're using the jQuery validate plugin then the valid() method returns a boolean to indicate whether or not the form validated successfully, so you could just do:

if ($("#myform").valid()) {
    CallSomeOtherFunction();
}
like image 176
Stelloy Avatar answered Nov 26 '22 12:11

Stelloy