Requirement: Input the maximum number of characters is 8. The maximum number of Characters can be entered in English. However, the maximum number of characters can be entered in Chinese.
How to solve the problem that a maximum of 8 entries are entered for both English and Chinese characters instead of counting by bit?
<! DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <input type="text" name="input1" Id =" STR "value=" placeholder=" placeholder "> <input type="text" name="length" value=" id="leng" </button> <script type="text/javascript"> var GetLength = function() {var STR = document.getElementById("str"); var str1 = str.value; //<summary> get the actual length of the string, </param> var realLength = 0, len = str1.length, charCode = -1; for (var i = 0; i < len; i++) { charCode = str1.charCodeAt(i); if (charCode >= 0 && charCode <= 128){realLength += 1; }else{ realLength += 2; } } var objLength = document.getElementById("leng"); objLength.value = realLength; }; / / execute code: / / alert (JMZ. GetLength ($(' # userid). Val ())); </script> </body> </html>Copy the code