Js Code that limits the length of each line input string in textarea

Source: Internet
Author: User

However, textarea does not have this attribute.

The textbox attribute of asp.net server cannot be used, so we only need to use js scripts to control it.
Okay. Let's talk about the code first.

Source code:

Copy codeThe Code is as follows:
Function textCounter (field, maxlimit, lines) {// parameter description: field is a textarea object, maxlimit is the maximum allowed length, and lines is the number of rows
Var arr = field. value. split ("\ n"); // first, we need to verify the number of lines by passing the "\ n"
Var perLine = "";
Var value = "";
If (arr. length <lines) lines = arr. length; // determine whether the number of rows exceeds the number of rows. If the number exceeds the limit, change the number of rows to the limit, because we need to calculate the length of the string.
For (loop = 0; loop <lines; loop ++) {// The total length of the loop Measurement string.
PerLine = arr [loop];
If (perLine. length> maxlimit)
PerLine = perLine. substring (0, maxlimit );
Value = value + perLine;
If (loop! = Lines-1)
Value = value + "\ n ";
}
If (field. value! = Value)
Field. value = value;
If (checkstr (value, maxlimit) {// determines if the string length exceeds the standard
Field. value = value. substring (0, maxlimit); // Delete the extra strings that are exclusive to each other.
}
}

Function showOverWords (obj, maxlength) {// display the remaining number of input characters. obj is a txteara object, and maxlength is the maximum length.
Len = obj. value. length;
$ ("# WordCount" ).html (maxlength-len); // This sentence is jquery and can be changed by yourself. Indicates modifying the id to the value of the wordCount tag.
}

Function checkstr (str, digit) {// determines whether the length of a string exceeds the standard in Chinese and English.
Var n = 0;
For (I = 0; I <str. length; I ++ ){
Var leg = str. charCodeAt (I); // ASCII code
If (leg> 255) {// all the words above 255 are in Chinese
N + = 2; // if it is Chinese, it is 2 bytes.
} Else {
N + = 1; // english.
}
}
If (n> digit ){
Return true;
} Else {
Return false;
}
}


OK, there are three functions on it. We can call these three functions to implement the functions we want.

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.