Javascript simple method to limit the number of characters input in textarea _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.