javscript send ajax request only if submit is valid 1

javscript send ajax request only if submit is valid

$( document ).ready( function(){setupForms();} );

function setupForms(){

    var ajaxOptions = { beforeSubmit:checkForm,
                        success:function(){
                               // the ajax was successful
                       }
                      };

    $( '.ajaxForm' ).ajaxForm( ajaxOptions );   
    $( '.ajaxForm' ).validate();
}

function checkForm(data,form){
    var valid = $(form).valid();
    return valid;
}   

Here is what the above code is Doing:
1. When the document is ready, it calls the setupForms() function.
2. The setupForms() function sets up the ajaxForm() plugin for all forms with the class “ajaxForm”.
3. The ajaxForm() plugin is set up to call the checkForm() function before submitting the form.
4. The checkForm() function calls the validate() function on the form.
5. If the validate() function returns true, the form is submitted.
6. If the validate() function returns false, the form is not submitted.

Similar Posts