JQuery If myform.valid () is true, then run another jQuery function?

I have a jQuery function that I would like to execute only if the form validation is true.
I have validation and other jQuery functions.

the problem is that both checking and another function are performed at the same time.

I would like to run another function only if the form validation is correct. 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 do this?

+5
source share
3 answers

If you use a standard plugin , this code should be supported:

$(document).ready(function() {
    $("#myForm").validate({ 
        submitHandler: function(form) {
            SomeFuncHere();
        }
    });
})

After successful verification, you should call the code submitHandler.

: :

$("#myform").validate({ 
    submitHandler: function(form) {
        var cboxes = ($('input:checkbox:checked').filter(":checked").length);
        var nboxes = ($(":checkbox:not(:checked)").length);
        var flag = false;
        if ((cboxes > 0) && (nboxes > 0)) {
            flag = 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.');
        }
        else {
            flag = true;
        }
        return flag;
    }
});
+3

jQuery, valid() , , , :

if ($("#myform").valid()) {
    CallSomeOtherFunction();
}
+5

You have to use .validate().form()

if (!$("#myform").validate().form()) {
    return false; //doesn't validate
}else
{
    //form is validated do your work
}
+1
source

All Articles