开发者

Limiting Keywords with jQuery by counting commas?

开发者 https://www.devze.com 2023-01-19 17:15 出处:网络
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 ca

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号