Custom Validation on jquery validate plugin, need to count element in a multiple select
Posted
by 0plus1
on Stack Overflow
See other posts from Stack Overflow
or by 0plus1
Published on 2010-04-27T13:17:39Z
Indexed on
2010/04/28
0:33 UTC
Read the original article
Hit count: 360
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!
© Stack Overflow or respective owner