1

I'm trying to use a custom validation function to check if at least one check box is selected. But the validation function is not firing. What am I missing?

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
    <script type="text/javascript" src="../Scripts/jquery-1.8.0.js"></script>
    <script type="text/javascript" src="../Scripts/jquery.validate.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.validator.addMethod("fruits", function(value, elem, params) {
                if($(".fruits:checkbox:checked").length > 0){
                    return true;
                }else {
                    return false;
                }
            },"pick at least one plz");​

            $("#form1").validate({
                rules:{
                    fruits:{
                        fruits: true
                    }
                },
                submitHandler: function(form) {
                    alert("submitting...");
                    form.submit();
                },
                invalidHandler: function(form){
                    alert("invalid form");
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1">
    <input type="checkbox" class="{fruits: true}" name="fruits" id="chkApple" value="1" />Apple<br />
    <input type="checkbox" class="fruits: true}" name="fruits" id="chkBanana" value="2" />Banana<br />
    <input type="submit" value="Submit" />
    </form>
</body>
</html>
3
  • Are you sure that the scripts are linked correctly? Commented Aug 16, 2012 at 5:41
  • I had once problem with the jquery validation plugin. I had to use a bit lower version of jquery for that Commented Aug 16, 2012 at 5:42
  • Does this answer your question? Can I use jQuery to check whether at least one checkbox is checked? Commented Nov 24, 2020 at 23:02

4 Answers 4

0

try:

$.validator.addMethod("fruits", function(value, elem, params) {
  return $(".fruits").is(":checked")
},"pick at least one plz");​
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is missing its educational explanation.
0

replace your code with this

if($(".fruits").is(":checked")){

1 Comment

This answer is missing its educational explanation.
0

check this link source

 $(document).ready(function(){
  $('#submit_button').click(function() {
    if (!$("input[@name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('One of the  buttons is checked!');
    }
  });
});

1 Comment

This answer is missing its educational explanation.
0

I got it working. The problem with not firing is caused by a problematic encoding issue. After that's resolved, the custom function is called. But $(".fruits").is(":checked syntax does work for me, it keeps saying nothing is selected. so I went old school with a for loop.

<html>
<head>
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.validator.addMethod("fruits", function (value, elem, params) {
                var items = $("input[name^='fruits']");
                for (i = 0; i < items.length; i++) {
                    if ($(items[i]).is(":checked")) {
                        return true;
                    }
                }
                return false;
            }, "pick one plz");

            $("#form1").validate({
                rules: {
                    fruits: {
                        fruits: true
                    }
                },
                submitHandler: function (form) {
                    alert("submitting...");
                    form.submit();
                },
                invalidHandler: function (form) {
                    alert("invalid form");
                }
            });
        });
    </script>
</head>
<form id="form1">
<input type="checkbox" class="{fruits: true}" name="fruits" id="chkApple" value="1" />Apple<br />
<input type="checkbox" class="fruits: true}" name="fruits" id="chkBanana" value="2" />Banana<br />
<input type="submit" value="Submit" />
</form>
<body>
</body>
</html>

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.