微博输入框统计

按新浪微博的输入框的统计来看,特性有:

1、中文字符按1来算

2、英文字符按0.5来算

3、结果将四舍五入

 

        //处理全角字符和中文
var count = 0, reg = /[^\x00-\xff]|[\u4E00-\u9FA5]/, maxlen=150;
for (var idx in text) {
if(reg.test(text[idx])){
count+=1;
} else {
count+=0.5;
}
}

return Math.floor(maxlen-count);

 

字数统计完后剩下的就是展示了。

posted @ 2012-02-26 19:54  雪霁霜飞  阅读(272)  评论(0编辑  收藏  举报