views:

106

answers:

3

First let me thank all of you for your amazing efforts and help thus far. Now to the question.

I have five checkboxes and at least one of the five needs to be checked to validate as required.

I have seen a few methods of how to do this but I can't seem to make any of them work.

Solve One Solve Two

Now I am using both the validate plugin and metadata plugin. How can I easily say if one checkbox with class required_group is checked the rest are no longer required? I have also given all checkboxes class="required"

As always Thanks in Advance!

Edit: My apologies for improperly phrasing the question. I would use radio buttons or selects as it is quite easier but it is requested that I use checkboxes. Again thank you for your time and sorry for the confusion.

FINAL EDIT So here is how I did it in case anyone else ever needs to.

    jQuery.validator.addMethod('ClassAppliedToCheckBoxes', function(value, element, checked) {
            var $module = $(element).parents('.YourContainerClass');
            return $module.find('input:checked').length;
            });

            jQuery.validator.addClassRules("ClassAppliedToCheckBoxes", {
                    'ClassAppliedToCheckBoxes' : true
            });

            jQuery.validator.messages.required_group = 'Please check at least one of these fields.';
+2  A: 

Please don't do that. It's a horrible practice from the viewpoint of usability. As soon as users sees checkboxes, they assume there will be multiple selection. For single selection, we have radioboxes. Give all of them the same name, and they'll be exclusive.

Example:

I am a:<br/>
<input type="radio" id="age_old" name="age" value="old">
  <label for="age_old">Oldtimer</label><br/>
<input type="radio" id="age_mid" name="age" value="mid" checked>
  <label for="age_mid">Middle-aged</label><br/>
<input type="radio" id="age_young" name="age" value="young">
  <label for="age_young">Youth</label><br/>

Bonus: no JavaScript required - all plain HTML. If you need the ability to not select anything, just add another radio box with an empty value:

<input type="radio" id="age_no" name="age" value="">
  <label for="age_no">...not telling!</label><br/>

UPDATE: It has been pointed out that this is not what the question is about. If that is the case, and the other reading ("at least one checkbox needs to be set") is required, ignore this.

Amadan
re: usability, I think the user can select between 1 and 5. If they select 0, then the form submission should be stopped.
nickf
@nickf: ...huh? Neither myself nor OP mentioned 1-5, 0, nor stopping submission, that I saw...
Amadan
Yes, this is a case where "Don't do that" really is the right answer -- unless we're missing something here. You never need *more* than one item to be checked, right?
harpo
I understood the same that @nickf : `only one of the five needs to be checked to validate as required` plus checkboxes equals that OP implies that two or more *could* validate too. Thus it would be a case for select `MULTIPLE` ... if it wasn't so hard to use for common people, even with directions.
Felipe Alsacreations
@Amadan : plus the links Solve one and Solve two from OP are both questions that ask for "at least one ...". (meta: how much time do I have to edit a comment? sorry for double comment)
Felipe Alsacreations
@Felipe: If so then I completely misread the question. Sorry.
Amadan
@Felipe: Actually, rereading the question, I *didn't* misread it. If it was meant to be what you say, then it is plain *misleading*. I would have said, in the title, "at least one" instead of "one", and in the question "at least one of the five" instead of "only one of the five".
Amadan
@Amadan : you're welcome. Even if we can guess from the links, we shouldn't have to guess. You're perfectly right with that: it's misleading anf it should be written 'at least' somewhere in the question or better in the title. In fact I found out about the links only after my first comment ... And if having 3 answers and none answering the real question of OP isn't an indication, I don't know what it is :)
Felipe Alsacreations
My apologies I should have indicated at least one checkbox needs to be checked. I had it as radio buttons but it has been requested to be checkboxes. Sorry for the confusion.
CarterMan
A: 

Try something like this:

var $invalid = $(":checkbox.required").filter(function() {
    var n = this.name;
    return $(":checkbox[name='" + n "']:checked").length == 0;
});

From there, you could do a number of things. To halt form submission, check if the length is greater than zero (if so, there are some invalid ones). To highlight invalid ones:

$invalid.parent().css({ color: '#f00' });

Note that this isn't a very efficient algorithm: it performs the check on the same group as many times as there are checkboxes. If this is a bottleneck then there's a place you can improve, but it'll be a good starting point anyway.

nickf
+1  A: 

I agree with Amadan that checkboxes are not the way to go. Checkboxes are for multiple choice. If you're looking to make the form accessible, then make it a SELECT:

<label for="myselect">Choose type of house</label>

<select name="myselect" id="myselect" class="required">
  <option value="1">Option 1 Text</option>
  <option value="2">Option 2 Text</option>
  <option value="3">Option 3 Text</option>
  <option value="4">Option 4 Text</option>
  <option value="5">Option 5 Text</option>
</select>

This will make your form more kind to people who navigates it with a screen reader. Radio buttons causes some issues for people who tab through forms.

Gert G
I do agree `SELECT` is pretty much equivalent functionally - but radio boxes are closer to checkboxes visually, so I thought it a natural replacement. However (and, not being disabled, I might or might not be correct), a properly coded CB/RB is not any less navigable than a selectbox. I think you will see that in my example, even unknowingly I followed the recommendations from your link (use of `label`, text immediately following the RB). I do admit that RBs are often badly coded, but so is everything else. Sturgeon's law.
Amadan
I agree on that radio buttons are more visually appealing. The issue I'm addressing with the select is to make it easier to navigate with a screen reader (radio buttons tend to be a problem for these folks). If you ever get the chance, sit in on a session with a visually impaired person who's navigating the Web with a screen reader. It's truly an eyeopener and a learning experience.
Gert G
Sorry, the only visually impaired people I met were deaf-mute-blind people speaking Croatian averagely to badly, and English badly to not at all. I've never came in contact with a screen reader, seeing how Croatian is not that popular, and would not have been much of a help to those folk as it is (unless someone came up with a robotic arm with simultaneous translation into Croatian Sign). EDIT: Not sarcasm. I used to volunteer for a deaf-mute-blind organisation.
Amadan