views:

514

answers:

2

Hi guys,

I've got this function to check my form:

function checkFrm() {
    $.each($('select'), function() {
     var $this = $(this);

     if( $this.val() === 'null') {
      // do nothing
     } else {
      if($this.next('input').val().length < 1) {
       return false;
      }
     }
    });
}

When the user submits this, it runs this code, and ideally if the criteria is met the form won't submit because of the 'return false;' bit.

However, for some reason it's completley ignoring this!

If I set a return variable at the start like 'var toreturn = true;' then set 'toreturn = false' when the trigger is hit, then 'return toreturn;' right at the end it stops the form submitting just fine... however that's not much use, as the alerts and checks I run in between are all triggered at once which would be completely overwhelming for the user.

Any suggestions please?

Cheers :)

+7  A: 

Returning false from the each will not return false from the function.

You will need to set a var to return false from the function also. You can still break out of the each by using return false as soon as your condition fails.

function checkFrm() {
    var retVal=true;
    $.each($('select'), function() {
        var $this = $(this);

        if( $this.val() === 'null') {
                // do nothing
        } else {
                if($this.next('input').val().length < 1) {
                        //set the var to return from the function
                        retval = false; 
                        //exit out of the each
                        return false;
                }
        }
    });
    return retVal;
}
redsquare
Whoops, just added this exact answer. I will delete and +1 to you! My only other comment was a small FYI that returning false from $.each simulates a "break" in the iteration while returning true simulates a "continue." I always find that's an easy way to look at it.
Kevin Swiber
jquery calls your anonymous function for each of your form controls and than ignores the return code of it. (well... in reality "each" stops as soon as it sees a false return code... but there is no way "each" could tell you that it saw a false return code as it can only return the objects it had to iterate over)
Kalmi
Many thanks! the input is indeed right by the next... awesome reply, will implement it now, thank you!!
Nick
indeed it does yup.
redsquare
Nick you dont need the filter inside the next. It will only slow it down
redsquare
D'oh! Just realised what I did wrong with this too... I returned false within the if statement before I set the return variable, oops! Thanks again redsquare, will remove the filter now too!
Nick
+1  A: 

When you call return false; it refurns false for the function

function() {
        var $this = $(this);

        if( $this.val() === 'null') {
                // do nothing
        } else {
                if($this.next('input').val().length < 1) {
                        return false;
                }
        }
    }

That is not work for you. You better get an array of selects like this:

var selectsArray=document.getElementsByTagName("select");

and work with them in a loop.

for(var i=0; i< selectsArray.length;i++){
      if( selectsArray[i].value === 'null') {
                // do nothing
        } else {
                if(selectsArray[i].next('input').val().length < 1) {
                        return false;
                }
        }
}
Eldar Djafarov
Another great response, thank you.
Nick