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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: 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%.
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.
0

This was my solution :-)

Use:

<div id="list">
<input name="chkbox" type="checkbox" />
<input name="chkbox" type="checkbox" />
<input name="chkbox" type="checkbox" />
<input name="chkbox" type="checkbox" />
</div>

<input type="hidden" name="the_real_field_name" />

Then in jquery validate plugin block:

rules : {
       chkbox: "required"
        },

Then store the values as an array into a single hidden field like:

function updateInput() {
    var allVals = [];
    $('#list :checked').each(function() {
        allVals.push($(this).val());
    });
    $('#the_real_field_name').val(allVals);
}
$(function() {
    $('#list input').click(updateInput);
    updateInput();
});

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.