html
核心js代码
//字符串截取
function getByteval(val, max) {
var returnValue = '';
var bytevalLen = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match(/[^x00-xff]/ig) != null)
bytevalLen += 2;
else
bytevalLen += 1;
if (bytevalLen > max)
break;
returnValue += val[i];
}
return returnValue;
}
$('#txt').bind('keyup',function(){
var val=this.value;
if(val.replace(/[^x00-xff]/g,"**").length>14){
this.value=getByteval(val,14)
}
})
注意:代码中使用了jquery绑定事件,所以需要加入jquery框架。



