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/