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>jsDetermine the input string length(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 count for two characters, alphanumeric count for one) function getByteLen(val) { var len = 0; for (var i = 0; i < ; i++) { var a = (i); if ((/[^\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(); if (curr > 0) { ("checklen").innerHTML = (); } else { ("checklen").innerHTML = '0'; ("subject").readOnly = true; } } </script> </head> <body> <div class="pbt cl"> <textarea maxlength="80" onkeyup="checkLength(this)" accesskey="1" tabindex="11"></textarea> <span >You can also enter <strong style="color: #FF0000">80</strong> Characters </span> <span class="spn_flag_1" 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 I share with you. I hope you can give you a reference and I hope you can support me more.