This time I will bring you js to implement character limit Chinese characters = two characters, js to implement character limit Chinese characters = two characters What are the precautions , the following is a practical case, let’s take a look one time.
html
<input type="text" id="txt">
Core js code
//字符串截取 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) } })
Note: The code uses jquery bindingevent, so jqueryframework needs to be added.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Using js to implement car dashboard
##canvas and JS to implement dynamic clock animation
How to set up InstantClick to be compatible with MathJax and Baidu Statistics, etc.
The above is the detailed content of js implements character limit Chinese characters = two characters. For more information, please follow other related articles on the PHP Chinese website!