This article describes how to limit the number of input characters in textarea in JavaScript. it involves the skills related to JavaScript mouse response and keyboard event processing in textarea input box characters. it has some reference value, for more information, see the following example. Share it with you for your reference. The details are as follows:
In this example, JavaScript restricts the number of characters in an area to no more than 255. if it is redundant, it is truncated.
The code is as follows:
Script function getStringUTFLength (str) {var value = str. replace (/[\ u4e00-\ u9fa5]/g, ""); // replace Chinese characters with two spaces and return value. length;} function leftUTFString (str, len) {if (getStringUTFLength (str) <= len) {return str;} var value = str. substring (0, len); while (getStringUTFLength (value)> len) {value = value. substring (0, value. length-1);} return value;} function count () {var len = 255; var value = document. getElementById ("licenseother "). value; if (getStringUTFLength (value)> = len) {document. getElementById ("licenseother "). value = leftUTFString (document. getElementById ("licenseother "). value, len);} document. getElementById ("result "). value = len-getStringUTFLength (document. getElementById ("licenseother "). value);} script
|
The input box contains a maximum of 255 characters (two Chinese characters: |
I hope this article will help you design JavaScript programs.