views:

858

answers:

1

I have a set of 4 checkboxes, all with different names, and require that at least 1 is checked.

I have set the class on all of them to 'require-one'.

<html>
<head>
<script src="scripts/lib/jquery.js" type="text/javascript"></script>
<script src="scripts/jquery.validate.js" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript">
 $(document).ready(function(){
    $("#itemForm").validate({

rules: { 
    check1: {  
        required : {  
            depends: function(element) {  
                $('.require-one:checked').size() == 0; 
            }  
        } 
    } 
}
    });
  });
</script>

</head>
<body>
<form name="itemForm" id="itemForm" method="post">
<input type="checkbox" name="check1" id="check1" class="require-one" value="1" />
<input type="checkbox" name="check2" id="check2" class="require-one" value="2" />
<input type="text" class="required" />
<input type="submit" />
</form>
</body>
</html>

If you put in 'return' before the $('.require-one:checked').size() == 0; However, now my problem is the error message will only disappear if Checkbox #1 is selected. If Checkbox #2 is selected it will not disappear, but will submit. How do I remove the error if any of the checkboxes are checked?

rules: { 
    'nameOfAnyCheckbox': {  
        required : {  
            depends: function(element) {  
              return $('.require-one:checked').size() == 0; 
            }  
        } 
    }
A: 

Swatting a fly with a rocket launcher?

$('.require-one:checked').size() == 0;

Apply the rule on any one of the checkboxes using it's name. For the form to be valid, this checkbox must pass validations. For this checkbox to be valid, at least one of the 4 checkboxes must be checked.

$("#itemForm").validate({
rules: { 
    'check1': {  
        required: {
            depends: function(element) {
                return $('.require-one:checked').size() == 0;
            }
        } 
    } 
}
});

Updated 2:

http://jsfiddle.net/MkPtP/1/

Anurag
That looks amazing! I am new to jQuery though. How would I put that into the jQuery code instead of an onClick event?
Michael
check the jsfiddle link - http://jsfiddle.net/zQg8m/. do you have to integrate this with the validation plugin?
Anurag
Yes, that's what I meant, sorry! With the validation plugin. Do I put it under a Rules heading?
Michael
The code makes sense, but is not working (see my edit above). It will validate the text box, but not the checkboxes.
Michael
I haven't used jquery validation before, but it has something to do with the use of `depends`. I've removed that and just used a callback function on `required`. Check updated code. link on jsfiddle is updated too.
Anurag
btw, the above code looks correct.. pasted the same thing on http://jsfiddle.net/MkPtP/1/ and it works.. i was wrong about not using `depends`
Anurag
Yes, it for sure works (but jsfiddle should be '==' not '>'). However, now the error message will only disappear if Checkbox #1 is selected. If Checkbox #2 is selected it will not disappear, but will submit. How do we remove the error class when any of the checkboxes are selected?
Michael
Yeah, doesn't look like a valid solution. I'm trying to figure out the same thing...
Trevor Hartman