The documentation for the validator options seems to have everything that you might need.
I’ve put together three different examples below, that cater to different types of needs that you may have:
- checkboxes all have the same name
- checkboxes have different names with different rules and/or messages
- checkboxes have different names with the same rules and messages
Checkboxes with the same name
For example, I’ve created a sample form:
<form id="checkboxTest">
<p id="itemErrors"></p>
<p><label><input type="checkbox" name="item[]" value="item 1"> Item #1</label></p>
<p><label><input type="checkbox" name="item[]" value="item 2"> Item #2</label></p>
<p><label><input type="checkbox" name="item[]" value="item 3"> Item #3</label></p>
<p><input type="submit" name="submit" value="Submit">
</form>
That uses the following rules:
rules: {
"item[]": {
required: true,
minlength: 2
}
},
Which replaces the default messages with these custom ones:
messages: {
"item[]": {
required: "Please select at least one item.",
minlength: "Please choose more than one"
}
},
And tells the errors for those checkboxes to appear together in a custom location:
errorPlacement: function (error, el) {
if (el.attr('name') === 'item[]') {
error.appendTo('#itemErrors');
} else {
error.appendAfter(el);
}
}
The overall result can be seen at this jsfiddle.
But what if your checkboxes are using different names?
Checkboxes with different names
Here’s another example where the checkbox names are similar:
<form id="checkboxTest">
<p id="itemErrors"></p>
<p><label><input type="checkbox" name="item1" value="item 1"> Item #1</label></p>
<p><label><input type="checkbox" name="item2" value="item 2"> Item #2</label></p>
<p><label><input type="checkbox" name="item3" value="item 3"> Item #3</label></p>
<p><input type="submit" name="submit" value="Submit">
</form>
We can tell the rules that each item will be required, but only if there are not enough items already checked.
rules: {
item1: {
required: {
param: true,
depends: notEnoughItems
}
},
item2: {
required: {
param: true,
depends: notEnoughItems
}
},
item3: {
required: {
param: true,
depends: notEnoughItems
}
}
},
There is some duplication here, and with the following messages, but we’ll soon see that removed.
messages: {
item1: {
required: "Please select at least two items."
},
item2: {
required: "Please select at least two items."
},
item3: {
required: "Please select at least two items."
}
},
The separate items are all grouped together, for easier error message handling:
groups: {
items: "item1 item2 item3"
},
and if their errors match, we’ll place them in the special place for them:
errorPlacement: function (error, el) {
var groups = $(el.get(0).form).validate().groups;
if (groups[el.attr('name')] === 'items') {
error.appendTo('#itemErrors');
} else {
error.insertAfter(el);
}
}
You can see this second example in action at jsfiddle
But how can we reduce duplication of the rules and messages?
Reduced duplication
We can prepare a few variables at the start, for the items and the item names.
var items = $('[name^="item"]'),
itemNames = [];
items.each(function () {
itemNames.push(this.name);
});
The validate() function doesn’t have the options for rules and messages any more. Those will be added later.
The groups in the validate() options can now be put together automatically from the itemNames.
$('#checkboxTest').validate({
groups: {
items: itemNames.join(' ')
},
And lastly we get to add the rules and messages, by looping through each item and adding the rules and messages on to each one individually.
function notEnoughItems() {
return items.filter(':checked').length < 2;
}
items.each(function (index, item) {
$(item).rules('add', {
required: {
param: true,
depends: notEnoughItems
},
messages: {
required: "Please select at least two items."
}
});
});
This example can be seen working at jsfiddle