0

I want to validate multiple functions, and for each one returning false, do some animations.

Here's the code:

                function validarChido(){ 
                    var dedo = $('#dedo_pick');
                    if(dedo.children().size() < 2){  
                        dedo.animate({'background-color' : 'red'},200);
                        dedo.animate({'background-color' : '#EEE'},200);
                        return false;  
                    }  
                    else{return true;}  
                }

                function validarEx(){ 
                    var valEx = $('#rate1');
                    if(valEx.children().size() < 2){  
                        valEx.animate({'background-color' : 'red'},200);
                        valEx.animate({'background-color' : '#EEE'},200);
                        return false;  
                    }  
                    else{return true;}  
                }

$('#form').submit(function(){  
                    if( validarChido() && validarEx() )  
                            {return true }
                        else  
                            {return false; }
                    }); 

When I send the form, I only see the first function doing the color animation, obviously because the form validation IF statement doesn't evaluate the next function because the first resulted in false here if( validarChido() && validarEx() )

Is there a way to evaluate all the functions and see the animation in each one and then send the form if everything is true?

3 Answers 3

1

Instead of returning false or true do something similar to the following:

function validarChido() {
                var dedo = $('#dedo_pick');
                if(dedo.children().size() < 2){  
                    dedo.animate({'background-color' : 'red'},200);
                    dedo.animate({'background-color' : '#EEE'},200);
                    return "error";  
                }  
                else{return "";}  
            }

            function validarEx(){ 
                var valEx = $('#rate1');
                if(valEx.children().size() < 2){  
                    valEx.animate({'background-color' : 'red'},200);
                    valEx.animate({'background-color' : '#EEE'},200);
                    return "error";  
                }  
                else{return "";}  
            }

$('#form').submit(function(){  
     var check = validarChido();
     check += validarEx();

                if( check == "" )  
                        {return true }
                    else  
                        {return false; }
                }); 
Sign up to request clarification or add additional context in comments.

Comments

1

It can be something like this:

$('#form').submit(function(){  
    var validarChidoResult = validarChido(),
        validarExResult = validarEx();

    if( validarChidoResult && validarExResult )  
        {return true }
    else  
        {return false; }
}); 

1 Comment

Thats Works for me!
0
var result = validarChido();
result = validarEx() && result;
return result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.