<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<script src= "Jquery-3.2.1.min.js" ></script>
<script>
$ (function () {
$ (' input[type= ' text "] '). On (' Input ', function (e) {
var $that = $ (this);
var Limitlen = $that. attr ("maxcodelength")//define the required number of bytes
$that. attr (' maxlength ', Limitlen);
SetTimeout (function () {
var value = $that. val (),
Reg =/[\u4e00-\u9fa5]{1}/g,//Chinese
Notreg =/\w{1}/g; Non-Chinese
var RESULTCN = Value.match (reg);
var resulten = Value.match (Notreg);
if (RESULTCN) {
Limitlen = Limitlen-(resultcn.length*2);
}
if (Resulten) {
Limitlen = Limitlen-resulten.length;
}
if (limitlen<=0) {
var finallen = Value.length+limitlen;
Value = value.substring (0,finallen);
$that. attr (' maxlength ', Limitlen);
$that [0].value = value;
}
},0);
});
});
</script>
<body>
Please enter: <input type= "text" maxcodelength= "ten" >
<input type= "Text" maxcodelength= "5" >
</body>
jquery extension Implementation input box character length limit Chinese 2 characters, English 1 characters