0

Hello there i have a problem. Suppose i have two type checkbox(person and company). If i submit form without checked any person or company checkbox then jQuery give me alert. Otherwise submit form. Here is my code given..

$(document).ready(function(){
$('#submit').click(function() {

    if( $('.com:checked').length < 1 .or $('.son:checked').length < 1 ) {
        alert('Please select at least one category');
        $('form').submit(function(){
            return false;
        });

    } else {
        $('form').unbind('submit');
    }
});
1
  • What exactly is the problem? Do you see any messages in the browser's console? Commented Aug 23, 2013 at 14:17

3 Answers 3

2

You are probably using wrong or operator .or should be || and swap the statements in condition then and else part.

if( $('.com:checked').length < 1 || $('.son:checked').length < 1 ) {
    alert('Please select at least one category');
} else {
    $('form').submit();
}
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function(){
$('#submit').click(function(e) {

e.preventDefault();

    if( $('.com:checked').length ) {
         $('form').submit();


    } else {
        alert('Please select at least one category');

    }
});

reference event.preventDefault
submit

Comments

0
$(document).ready(function () {
    $('#submit').click(function () {
        e.preventDefault;
        if ($('.com:checked').length && $('.son:checked').length) {
            $("form").trigger('submit');
        } else {
            $('form').unbind('submit');
        }
    });
});

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.