通过js实时统计文本框的输入字数限制 2022-07-15 80酷酷网 80kuku.com 限制文本框输入的字符数,若达到数目则停止其输入!以下是HTML网页特效代码,点击运行按钮可查看效果: 以下是程序代码<script>function checklen(obj){var lenE = obj.value.length;var lenC = 0;var CJK = obj.value.match(/[\u4E00-\u9FA5\uF900-\uFA2D]/g);if (CJK != null) lenC += CJK.length;tno.innerText = obj.maxlength - lenC - lenE ;if (tno.innerText < 0) {var tmp = 0var cut = obj.value.substring(0, obj.maxlength);for (var i=0; i<cut.length; i++){tmp += /[\u4E00-\u9FA5\uF900-\uFA2D]/.test(cut.charAt(i)) ? 2 : 1;if (tmp > obj.maxlength) break;}obj.value = cut.substring(0, i);}}</script><body onload="tno.innerText = s1.maxlength;"><textarea name="s1" rows="5" cols="50" maxlength="10" onpropertychange="checklen(this)"></textarea>共可输入10字符,还剩 <span id="tno"></span>个字符</font><a href="http://js.alixixi.com/">欢迎访问阿里西西网页特效代码站,js.alixixi.com</a> [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]