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.