Search code examples
jqueryformssubmission

jquery allow form submission after cancelled submission


i got a jquery function that controls that all the required fields of my form are filled and it cancels submission if one of the required fields are empty.

Canceling the form submission works fine, problem is that when i submit my form after a failed submission it stills cancels the form submission, when it should allow the submit because all the form is filled correctly, here is the code:

-Cancel is a bool that indicates if all the form fields are filled.

    if (cancel) 
    {
        $('#form_login').submit(function () 
        { 
            return false;   
        }); 
    }
    else
    {

        $('#form_login').submit(function () 
        { 
            return true;
        }); 
    };

what am i doing wrong ??

thanks


Solution

  • You better call your validation code from within the submit method. For example, a method that does some validation and return true or false. You can return that value as the submit method return value.

    For example:

    $('#form_login').submit(function ()  
    {  
       return isMyFormValid();    
    });
    
    function isMyFormValid(){
      //return true or false depending on some conditions.
    }