Ankur Saxena
Ankur Saxena

Reputation: 639

jquery validation checks not working properly

$(document).ready(function(){
    $(".submit").click(function(){
                if($('.disrtrict')[0].selectedIndex<=0){
                    $("#error1").html("district field is empty");

                } 

                if($(".block")[0].selectedIndex<=0){
                    $("#error2").html("block field is empty");  
                }


                if($(".village")[0].selectedIndex<=0){
                    $("#error3").html("village field is empty");    
                }

            });
});

above is my jquery script.i am new to jquery and try to learn jquery. my problem is where i use return false.what the concept of return true and false and i use if($(".block")[0].selectedIndex<=0) can we use if($(".block").val()==" ") if any one have standard checks please provide help.thank in advance.

Upvotes: 1

Views: 69

Answers (3)

palaѕн
palaѕн

Reputation: 73906

You can do this:

$(".submit").click(function () {
    if ($('.disrtrict').val() === '') {
        $("#error1").html("district field is empty");
        return false;
    }
    if ($(".block").val() === '') {
        $("#error2").html("block field is empty");
        return false;
    }
    if ($(".village").val() === '') {
        $("#error3").html("village field is empty");
        return false;
    }
    return true;
});

FIDDLE DEMO

Upvotes: 1

sathish
sathish

Reputation: 800

Inside your if condition, after the error message, you can use return false to avoid form submit..

if($(".block").val()=="")

The above code will work, if your first option value is empty.

Upvotes: 1

Rohit Agrawal
Rohit Agrawal

Reputation: 5490

if its a form submission the return true will allow form to submit the form later executing of javascript and return false will not submit the form after javascript

now regarding jQuery validation , yes you can use if($(".class").val()=='') for empty or non selection

Upvotes: 2

Related Questions