How to validate ToCheckList jQuery plugin (or multiple select fields)?
Anyone ever use the toChecklist jQuery plugin? It basically turns a multi-select box into a list of c开发者_开发知识库heckboxes. Makes it easier for users to understand that they can choose more than one item - excellent plugin.
However, I'm trying to make sure that users on my site pick at least 1 item in the list. ToChecklist has the capability of limiting the selection to a max value, but not minimum.
I'm already using the jQuery validate plugin to validate the whole form, any ideas to get it to work against a multi-select box (which would then hopefully just work with toChecklist)?
Thanks in advance!
The .toCheckList()
still leaves the original <select>
options intact, just hidden. This gives you the selected count using the :selected selector:
If you called $("#myList").toCheckList()
, the code to get selected count would be:
$("#myList :selected").length
Used like:
if($("#myList :selected").length > 0)
alert("Some options are selected!");
else
alert("Oh snap! better pick something");
You can use the length property to see how many elements there are in the array. (Every jQuery object is also an array)
$(anything).length
http://api.jquery.com/length/
if ($("#mychecklist").length > 0)
{
// valid - has 1 or more.
}
else
{
// invalid - has 0 items.
}
精彩评论