限制input输入框的字数

如:限制输入500字

html:

<input id="" name="" onkeyup="checkLength(this,1000)">

js:

 checkLength=function(dom,maxLength){

 var  l=0;

for(var i=0;i<dom.value.length;i++){

if(/[\u4e00-\u9fa5]/.test(dom.value[i]))

{

l+=2;

}else{

l++;

}

if(l>maxLength){

dom.value=dom.value.substr(0,i);

}

}

}

猜你喜欢

转载自blog.csdn.net/weixin_41175479/article/details/82454715