Custom Validation on jquery validate plugin, need to count element in a multiple select
- by 0plus1
I have a multiple select, and I need to force the user to choose maximum two options, nothing more.
I'm trying this:
jQuery.validator.addMethod("morethantwo",
function(value, element) {
var foo = [];
$(element+' :selected').each(function(i, selected){
foo[i] = $(selected).text();
alert(foo[i]);
});
return true;
},"Max two options."
);
The problem is that I get a:
uncaught exception: Syntax error, unrecognized expression: [object HTMLSelectElement]
error. While if I do this:
$(element).each(function(i, selected){
foo[i] = $(selected).text();
alert(foo[i]);
});
It works but I get all the options in the select. Why is that? Is this the correct road to walk? Are there better ways to do this kind of check?
Thank you very much!