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>
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
  • Are you sure that the scripts are linked correctly? – Adam Merrifield Aug 16 '12 at 05:41
  • I had once problem with the jquery validation plugin. I had to use a bit lower version of jquery for that – Parag Aug 16 '12 at 05:42
  • Does this answer your question? [Can I use jQuery to check whether at least one checkbox is checked?](https://stackoverflow.com/questions/2684434/can-i-use-jquery-to-check-whether-at-least-one-checkbox-is-checked) – mickmackusa Nov 24 '20 at 23:02

4 Answers4

0

try:

$.validator.addMethod("fruits", function(value, elem, params) {
  return $(".fruits").is(":checked")
},"pick at least one plz");​
Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72
0

replace your code with this

if($(".fruits").is(":checked")){
gaurang171
  • 9,032
  • 4
  • 28
  • 30
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!');
    }
  });
});
Parag
  • 4,754
  • 9
  • 33
  • 50
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>
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137