校验字符长度

计算字符长度
function checkCharacterLength(str){
    var len = 0;
    for(var y=0;yvar strLength = str.charCodeAt(y);
        if(strLength >= 0 && strLength <= 128){
            len += 1;
        }else{
            len += 3;
        }
    }
    return len;
}
var characterLength = 0;
characterLength = checkCharacterLength($("#id").val().trim());
if(characterLength > 20){
    alert("字符超出范围");
    return false;
}

你可能感兴趣的:(js)