输入框限定字符的个数(一个汉字算两个字符)

5年以前  |  阅读数:424 次  |  编程语言:JavaScript 
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)

}

})

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8