js 代码
function checkLen(obj,len){
if(obj.value.replace(/[^/x00-/xFF]/g,'**').length>=len){
obj.value=leftUTFString(obj.value,len);
}
}
function getStringUTFLength(str) {
var value = str.replace(/[^/x00-/xff]/g," ");
return value.length;
}
function leftUTFString(str,len) {
if(getStringUTFLength(str)<=len)
return str;
var value = str.substring(0,len);
while(getStringUTFLength(value)>len) {
value = value.substring(0,value.length-1);
}
return value;
}
调用方法:(onKeyUp(this,字符长度)) 如下:超过1000个字符(或者500个汉字,文本框将无法输入,复制粘贴也将自动截取1000个字符后面的内容)
<textarea cols="65" rows="4" name="txtRemarkItem" id="txtRemarkItem" onKeyUp="checkLen(this,1000)"></textarea>