0

I have a code block that checks certain elements before allowing a form to submit, the loop loops through OK.

I would anticipate, or at least had hoped that the first time it finds a problem, an alert is shown and then execution stops. What is actually happening is that alerts show multiple times, right down to the last one, and only the last one actually returns false!

I don't understand why this is, what am I doing wrong? The JavaScript is below.

$('#deliverInForm').submit(function(){
    $('.childRow').each(function(){
        if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
            if($(this).find('.thisBinName').val()==''){
                alert('You have entered a quantity but no Bin, please correct and try again!');
                return false;
            }else if($(this).find('.unitQty').val()==''){
                alert('You have entered a Bin but no quantity, please correct and try again!');
                return false;
            }else if($(this).find('.unitQty').val()==0){
                alert('Can\'t move zero units into a bay, please correct and try again!');
                return false;
            }else if($(this).find('.unitQty').val()<0){
                alert('Can\'t deliver in minus units into a bay, please correct and try again!');
                return false;
            }
        }
    });

    if($('#supDocNo').val()==''){
        alert('Can\'t leave Supplier Reference blank, please correct and try again!');
        return false;
    }
    return true;
});

5 Answers 5

4

make your function take an event argument, then call event.preventDefault()

e.g.

$('#deliverInForm').submit(function(event){
$('.childRow').each(function(){
    if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
        if($(this).find('.thisBinName').val()==''){
            alert('You have entered a quantity but no Bin, please correct and try again!');
            event.preventDefault();
            return false;
        }else if($(this).find('.unitQty').val()==''){
            alert('You have entered a Bin but no quantity, please correct and try again!');
            event.preventDefault();
            return false;
        }else if($(this).find('.unitQty').val()==0){
            alert('Can\'t move zero units into a bay, please correct and try again!');
            event.preventDefault();
            return false;
        }else if($(this).find('.unitQty').val()<0){
            alert('Can\'t deliver in minus units into a bay, please correct and try again!');
            event.preventDefault();
            return false;
        }
    }
});

if($('#supDocNo').val()==''){
    alert('Can\'t leave Supplier Reference blank, please correct and try again!');
    event.preventDefault();
    return false;
}
return true;
});
Sign up to request clarification or add additional context in comments.

2 Comments

The problem with this method is that the final check (if($('#supDoc...) will still be performed, potentially resulting in multiple alerts.
Simple to resolve that, rather than alerting on error, put the alert message into a variable, check if the variable is empty, if not, alert its contents. Or put the first alert before the foreach ;)
3

The problem is that inside your each loop, the return false just exits the function you have passed to each. According to the docs, this will exit the each loop, but it wont exit your submit handler.

You could do something ugly like this:

$('#deliverInForm').submit(function(){
  var exit = false;
  $('.childRow').each(function(){
    if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
      if($(this).find('.thisBinName').val()==''){
        alert('You have entered a quantity but no Bin, please correct and try again!');
        exit = true; // <----------------set flag
        return false;
        //...
  });

  if(exit) return false;
  // ...
});

Comments

0

from jquery each documentation

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

I think you must move your return false to the each loop and not inside the find()

Comments

0

Try this:

$('#deliverInForm').submit(function(){
    var errLog = '';
    $('.childRow').each(function(){
        if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
            if($(this).find('.thisBinName').val()==''){
                errLog += 'You have entered a quantity but no Bin, please correct and try again!\n';
            }
            if($(this).find('.unitQty').val()==''){
                errLog += 'You have entered a Bin but no quantity, please correct and try again!\n';
            }
            if($(this).find('.unitQty').val()==0){
                errLog += 'Can\'t move zero units into a bay, please correct and try again!';
            }
            if($(this).find('.unitQty').val()<0){
                errLog += 'Can\'t deliver in minus units into a bay, please correct and try again!';
            }
        }
    });

    if($('#supDocNo').val()==''){
        errLog += 'Can\'t leave Supplier Reference blank, please correct and try again!\n';
    }

    if( errLog != '' ) {
        alert( errLog );
        errLog = '';
        return false;
    }
    return true;
});

Hope it helps.

Comments

0

the event handler function needs to return the value.

$(".link").submit(function(){
    return false;
});

To show the first error dialog and return false you have to do something like this...

$(".link").submit(function(){
    var submit=true;
    $("div").each(function(){
        if(submit) // comment this out to show all dialogs
            if(!condition){
                submit = false; 
                alert("problem");
                return ""; // return here is irrelevant

            }
    })
    return submit;
});

One more thing, if your function throws an error it will not return false and the submit will occur anyway...

$(".link").submit(function(){
    throw new Error("foo");
    return false; // never happens form submits as if returning true;
});

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.