0

I have lots of functions which is used for validations. Can I use same return varibale for all the functions?

function fn1() {
    if () {..
        first = true;
    } else {
        first = false;
    }
    return first;
}

function fn2() {
    if () {..
        second = true;
    } else {
        second = false;
    }
    return second;
}

function fn3() {
    if () {..
        three = true;
    } else {
        three = false;
    }
    return three;
}


$('#submitButton').click(function () {
    var returnVal = true;
    returnVal1 = fn1();
    returnVal2 = fn2();
    returnVal3 = fn3();

    if (returnVal1 && returnVal2 && returnVal3) {
        returnVal = true;
    } else {
        returnVal = false;
    }
    return returnVal;
});

Instead of these many variables like returnVal1, returnVal2 and returnVal3, can I use returnVal for all the function returns? Can I use a single variable for all the function returns?

Thanks

2
  • Just call one function? What exactly are you trying to do? Commented Nov 15, 2013 at 14:11
  • You can return true/false from your function Commented Nov 15, 2013 at 14:11

5 Answers 5

4

You can drop the variables all together and just call your validation functions inside the if clause:

$('#submitButton').click(function () {
    if ( first() && second() && third() ) {
       return true;
    } else {
       return false;
    }
});

or shorter as @FishBasketGordo suggested, if the return value is the only thing you need:

$('#submitButton').click(function () {
    return first() && second() && third();
});

If the validation is rather computation heavy, this also saves you some time, as the if-clause is evaluated lazely.

Sign up to request clarification or add additional context in comments.

1 Comment

Or just return first() && second() && third();
1

Yes you can, but if you want a single end variable that determines if any single one fails then I would recommend doing so like this:

var isValid = first();
if(!second())
    isValid = false;
if(!three())
    isValid = false;

That way you don't end up resetting an invalid flag with a subsequent result.

The benefits of this, is that if you have a lot of validation functions you can throw them into an array and write less code...

var validators = [first, second, three];//etc..
var isValid = true;
for(var i = 0; i < validators.length; i++){
    if(!validators[i]()){
        isValid = false;
        break;
    }
}
//do something with isValid

Or if you have a dedicated "validate all" function...

var validators = [first, second, three];
function validateAll(){
    for(var i = 0; i < validators.length; i++){
        if(!validators[i]()){ 
            return false;
        }
    }
    return true;
}

2 Comments

Yes..I have lots of validation functions..So in the first case..U mean I have to call all the functions inside and array? like var validators = [fn1, fn2, fn3, fn4]
@user1049057: Not call them inside the array, just declare them. But your example is correct: var validators = [fn1, fn2, fn3, fn4]; will work fine, but I guess you already know that based on you having accepted my answer
1

You can replace the last function by the following code

$('#submitButton').click(function(){
    return (first() && second() && third());
});

1 Comment

Damn thanks for the heads up @Sirko, bad copy/paste, heading to the coffee machine right now
1

If your returnValues are primitives that are going to be combined, you can easily combine their assignments. In your code snippet you can write it as below if you'd prefer:

$('#submitButton').click(function () {
    var returnVal = first() && second() && third();

    return !!returnVal; //ensure boolean is returned
});

6 Comments

I tried if(first && second && three){returnVal = true;}else{returnVal = false;}..But that doesnt seem to be working..
@user1049057 first second and three are functions. You have to call them
I mean the return variables of those function first, second and three..I am trying to put the return variables of those function inside if condition and checking
Make sure you dont have name conflicts with your variable names and functions. If you want to update your question I'll update this answer
yes..return variables and the function names are different. I just created this as a sample
|
0
function validateFunction(){   
  var result;     
  // First Function Condition\
  if(First Function)
      { 
        result = true;
      else
        result = false;
      }
      checkTruFalse(result);

  // Second Function Condition
  if(Second Function)
      { 
        result = true;
      else
        result = false;
      }
      checkTruFalse(result);

   // Third Function Condition
   if(Third Function)
       { 
         result = true;
       else
         result = false;
       }
       checkTruFalse(result); 
   }
 }

 function checkTruFalse(result)
   {   
     if (result == false ){ alert('ERROR'); break; } 
   }

 $('#submitButton').click(function(){ validateFunction(); });

As per the situation you can pass another variable inside checkTruFalse function to set alert message about specific error.

function checkTruFalse(result, message ){
  alert('ERROR :: '+message);
}

checkTruFalse( result, 'Invalid Input for Numeric Value');

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.