2

I want to return false or break the foreach loop. But it is not working. This is the code which i have tried.

 function validate()
    {
        var validate="false";
        var atLeastOneIsChecked =  $("input[name='city[]']").is(":checked");
        if(atLeastOneIsChecked==true){

            $("input:checkbox[name='city[]']:checked").each(function(j){
                var city=$(this);

                $('select[name="category[]"]').each(function(i){

                    if(i==j){
                         if($(this).val()=="0"){
                         alert("Please select hotel category");
                         city.click();
                           //validate="false";
                            return false;
                       }
                        else
                        {
                            validate="true";
                        }

                    }
    // return false;
                }); 
                alert(j);
               // return false;
            });
            //return false;
        }
        else
        {
            alert('Please select atleast one city.');
            validate="false";
        }
        if(validate=="true"){
            return true;
        }
        else
        {
            return false;
        }
    }

After the "Please select hotel category" the function should return false. But the form is posted after the alert. I don't what's wrong.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
asifa
  • 771
  • 1
  • 28
  • 63

3 Answers3

0

If I am not wrong, the mistake you are making is that you think this:

if($(this).val()=="0"){
    alert("Please select hotel category");
    city.click();        
    //validate="false";
    return false;
}

will prevent the click handler from posting the form. The return false part needs to happen in an event handler that is called before the one that submits the form.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • can you please tell how should i do that? – asifa Oct 24 '12 at 11:03
  • It is in a loop so for the first if it is false, then the validate variable is set to false,but second time if the condition is true then the validate is set to true and the earlier false is overwritten so to avoid it i wrote return false. Can you please show me how to do it the proper way. – asifa Oct 24 '12 at 11:07
  • After the foreach loop i did this if(validate=="false"){ return false; }Thanks – asifa Oct 24 '12 at 11:31
0

Put the handler in your form tag like so:

<form onsubmit="return validate();" method="POST" action="post.php">

  ..

</form>

Then it will get invoked on submit, and if you return false in your function, it will prevent the form from submitting.

Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
0

Thanks everyone. I modified my code

function validate()
    {
        var validate="false";
        var atLeastOneIsChecked =  $("input[name='city[]']").is(":checked");
        if(atLeastOneIsChecked==true){

            $("input:checkbox[name='city[]']:checked").each(function(j){
                var city=$(this);
                $('select[name="category[]"]').each(function(i){
                    if(i==j){
                        if($(this).val()=="0"){
                            alert("Please select hotel category");
                            city.click();
                            city.attr('checked', true);
                            validate="false";
                            return false;
                        }
                        else
                        {
                            validate="true";
                        }
                    }
                }); 
                if(validate=="false"){
                    return false;
                }
            });
        }
        else
        {
            alert('Please select atleast one city.');
            validate="false";
        }
        if(validate=="true"){
            return true;
        }
        else
        {
            return false;
        }
    }

Now it is working.

asifa
  • 771
  • 1
  • 28
  • 63