views:

63

answers:

1

I am having some difficulty in using the jQuery Validator plugin. I have a list of checkboxes with different name attributes and I can't seem to figure out how to ensure that at least one of them has been checked. Everything that I find on Google seems to only work when the name attribute is the same.

Here is some sample code (updated):

<ul id="email_lists">
    <li>
        <input name="checkbox1" type="checkbox" /> List 1
    </li>
    <li>
        <input name="checkbox2" type="checkbox" /> List 2
    </li>
    <li>
        <input name="checkbox3" type="checkbox" /> List 3
    </li>
    <li>
        <input name="checkbox4" type="checkbox" /> List 4
    </li>
</ul>

I want to make sure that at least one of those is checked. Unfortunately, I cannot make the names the same as it is form that submits to a third-party email marketing application and it is expecting specific name attributes for these checkboxes.

Update

I am aware of how to do this using plain jQuery, but I would prefer to use the jQuery Validator plugin since that is how all of the other validation on the page is done.

I can group those checkboxes using jQuery by saying $('#email_lists li');, but I'm not really sure how to use something like that and tell the jQuery Validator plugin to use that as a group.

+1  A: 

Assuming that you can give the checkboxes a class name (the jQuery needs something to work with):

<input class="validationGroupOne" name="checkbox1" type="checkbox" />
<input class="validationGroupOne" name="checkbox2" type="checkbox" />
<input class="validationGroupOne" name="checkbox3" type="checkbox" />
<input class="validationGroupOne" name="checkbox4" type="checkbox" />

You should be able to plug in the .validationGroupOne class-selector in place of the, usual, name attribute.

David Thomas
I can give them a class name or since I actually have them in an unordered list, I could so something along the lines of $('#idOfUL input[type=checkbox]'). Where could I find the documentation on .validationGroupOne?
Jared
@Jared: that was just a random class-name I used; I don't think it has any importance, or relevance, to anything outside of this example. And yeah, presumably you could access them via the `ul` just as easily. I've not worked with this plug-in though, so I'm not 100%.
David Thomas
Ah gotcha, I thought that was actually a rule or extra method that is part of the jQuery Validation plugin. I guess my question then is how would I then tell the jQuery Validation plugin to then use that group? That is where I think my confusion is.
Jared