限制输入长度,分全角半角,例如只能输入个14个英文字符7个中文字符
function CheckLength(txtObj,length) {
??? var str = txtObj.value;
??? var len = str.length;
??? var temp="";
??? var reLen = 0;
??? for (var i = 0; i < len; i++) {????
??????? if (str.charCodeAt(i) >255 ) {
??????????? // 全角??
??????????? reLen += 2;
??????? } else {
?????????? reLen++;
??????? }
??????? if (reLen < length+1){
??????? ?? temp+=str.charAt(i);
??????? }else{
??????? ?? txtObj.value= temp;
??????? }
???????
??? }
}
?
<input id="id" name="id" value="" onKeyUp="CheckLength(this,10)" />