js determines the input string length example code (Chinese characters count two characters, alphanumeric count one)
When entering text, this method of verification came to mind because the database table field length limit will cause the submission to fail.
Let's talk about nonsense and add the code:
<html><head> <title>js determines the length of the input string (Chinese characters count two characters, alphanumeric counts one)</title> <style type="text/css"> .pbt { margin-bottom: 10px; } .ie6 .pbt .ftid a, .ie7 .pbt .ftid a { margin-top: 1px; } .cl:after { clear: both; content: "."; display: block; height: 0; visibility: hidden; } </style> <script type="text/javascript"> //Get the length of the string (Chinese characters counts two characters, alphanumeric counts one) function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { var a = val.charAt(i); if (a.match(/[^/x00-/xff]/ig) != null) { len += 2; } else { len += 1; } } return len; } // As long as the keyboard is lifted, verify the text length in the edit box. The maximum character length can be set as needed function checkLength(obj) { var maxChars = 80;//Maximum number of characters var curr = maxChars - getByteLen(obj.value); if (curr > 0) { document.getElementById("checklen").innerHTML = curr.toString(); } else { document.getElementById("checklen").innerHTML = '0'; document.getElementById("subject").readOnly = true; } } </script></head><body> <div> <textarea id="subject" maxlength="80" onkeyup="checkLength(this)" accesskey="1" tabindex="11"></textarea> <span id="subjectchk">You can also enter <strong id="checklen" style="color: #FF0000">80</strong> characters</span> <span id="postNameRule" style="display: none"></span> </div></body></html>The above example code for JS judging input string length (Chinese characters count two characters, alphanumeric count one) is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.