How to limit the number of words entered in the textarea text box

Source: Internet
Author: User

How to limit the number of words entered in the textarea text box

How to limit the number of words entered in the textarea text box:
In practice, you often need to limit the length of the input words in the text box. The following describes how to implement this function through a code example.
The code example is as follows:

<! Doctype html> 

The above code implements our needs. We can limit the length of the input text to 10. Both keyboard input and copy and paste can be restricted. The following describes the implementation process.
I. Implementation principle:
When input with a keyboard, bind an event handler function to the keyup event, and then use this function to determine whether the text length of the current text box exceeds the specified value when the keyboard is started, if the screenshot is exceeded, otherwise the original length is maintained. When copying and pasting is used, you can determine whether the text length of the text box exceeds the specified value when the text box loses focus. If the value of the text box exceeds the specified value, the text will be automatically truncated.
Ii. Code comments:
1. $ (function () {}, the document structure is fully loaded before executing the code in the function.
2. $ ("textarea [maxlength]"). keyup (function () {}, register a keyup event handler for the textarea text box with the maxlength attribute.
3. var area = $ (this) to convert the current object to a jquery object.
4. var max = parseInt (area. attr ("maxlength"), 10) to obtain the maximum input length. See section 1 of parseInt () function in JavaScript.
5. area. val (area. val (). substr (0, max). To intercept a String, refer to the substr () method section of the String object in JavaScript.
The following code is similar.

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 8182.

For more information, see: http://www.softwhy.com/jquery/

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.