Limiting Keywords with jQuery by counting commas?
I'm using a CMS to allow users to add keywords to their profiles, but I'm wanting to limit that to 10 keywords, which are comma-del开发者_开发问答imited, by counting the number of commas (in this case, 9).
Any ideas on how to go about doing this with jQuery? I'll be doing a double-check on the server-side, but I'd like a quick little, live error checker on the front-end.
instead of counting commas, how about splitting them then get the length.
var count = 'reigel,me,you,we'.split(',').length; // results 4
fiddle to see.
and if you're using this in a <form>
, you could edit the submit handler like this,
$('#formID').submit(function(){
if ($('#inputTextId').val().split(',').length < 10) {
return false; // prevent page from submitting...
}
});
another fiddle
Welcome to stackoverflow.com
Don't forget to accept an answer
精彩评论