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

后端 未结 3 772
予麋鹿
予麋鹿 2021-02-09 16:40

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 b

相关标签:
3条回答
  • 2021-02-09 17:18

    If you're using standard plugin, such code should be supported:

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

    The code in submitHandler should be called after successful validation.

    Edit: based on your code, try this instead:

    $("#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;
        }
    });
    
    0 讨论(0)
  • 2021-02-09 17:32

    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();
    }
    
    0 讨论(0)
  • 2021-02-09 17:35

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

    if (!$("#myform").validate().form()) {
        return false; //doesn't validate
    }else
    {
        //form is validated do your work
    }
    
    0 讨论(0)
提交回复
热议问题