开发者

jQuery live counter for SPECIFIC chars in textarea

Googling is driving me mad; all jQuery textarea counters I can find either count words, newlines or characters.

I would like to count a specific character as it gets entered in the textarea. More specifically I would like to 'live count' the amount of @ signs that get inserted before the backend processes it. Live is nicer (with the counter and all) but I'll settle开发者_JS百科 for a onSubmit check too at this late hour ;-)

Does anybody know of a plugin / snippet that can do that?

Thanks!


Simple:

var atCount = 0;
var i;
textarea.onkeyup = function() {
   atCount = 0;
   for(i = 0; i < this.value.length; i++) {
       if(this.value.charAt(i) == '@')
          atCount++;
   }   
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜