日期:2014-05-16  浏览次数:20403 次

JS判断中英文字符长度,客户端验证,中文两个字节,英文一个字节

function countCharacters(str){
???? var totalCount = 0;
??? ?
???? for (var i=0; i<str.length; i++) {
???????? var c = str.charCodeAt(i);
???????? if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
???????????? totalCount++;
???????? }else {?? ?
???????????? totalCount+=2;
???????? }
??????? ?
???? }
???? return totalCount;
?? ?
}