Mansoor
Mansoor

Reputation: 130

form is submitimg even I am using return false

form is submitimg even I am calling return false my code

$(document).ready(function() {
    $('.submit').click(function() {
        $('.hour').each(function() {
            var $hours = $(this).val();
            if ($hours == "") {
                alert(" Active Time is required");
                return true;
            }
        });
    });
});​

submit button

 <input type="submit" class="submit" name="Submit" id="Submit" value="Submit" 
onclick="return validateForm();"/>

Upvotes: 0

Views: 52

Answers (4)

M. Amir Ul Amin
M. Amir Ul Amin

Reputation: 81

<script>

    $(document).ready(function(){
        $('.submit').click(function(e){ 
    var flag = false;
            $('.hour').each(function() {
                var $hours =  $(this).val();
                if (!flag && $hours == "")
                {
                    flag = true;
                    alert(" Active Time is required"); 
                    e.preventDefault();
                } 
            });   
        });
    });

</script>

Submit Button

<input type="submit" class="submit" name="Submit" id="Submit" value="Submit" />

Upvotes: 0

James Allardice
James Allardice

Reputation: 166001

You are not returning anything from the click event handler bound to the submit button. You can significantly shorten your code by using the .filter() method to reduce the set of .hour elements to those with no value, and you can return the result of a check of the length of the resulting set of elements from the event handler to allow/prevent form submission:

$(".submit").click(function () {
    // This return statement returns from the click event handler
    return $(".hour").filter(function () {
        // This one returns from the filter callback (once for each element)
        return this.value === "";
    }).length === 0;
});​

Here's a working example.

Upvotes: 0

LorDex
LorDex

Reputation: 2636

$('.submit').click(function() {
var r = false;
 $('.hour').each(function() {
            var $hours = $(this).val();
            if ($hours == "") {
                alert(" Active Time is required");
                r = true;
            }
        });
return r;
});

Upvotes: 0

xdazz
xdazz

Reputation: 160863

You need to return outside the .each callback function.

Upvotes: 1

Related Questions