Home  >  Article  >  Web Front-end  >  js code to limit the input string length of each line of textarea_form special effects

js code to limit the input string length of each line of textarea_form special effects

WBOY
WBOYOriginal
2016-05-16 17:48:471250browse

But textarea does not have this attribute.

The server-side textbox attribute of asp.net does not work, so we can only use js script to control it
Okay, without further ado, let’s start with the code

javascipt source code:

Copy code The code is as follows:

function textCounter(field, maxlimit, lines) {//Parameter description: field is a textarea object, maxlimit is the maximum allowed length, lines is the number of lines
var arr = field.value.split("n");//First we need to verify the number of lines by branching symbol" ”
var perLine = "";
var value = "";
if (arr.length < lines) lines = arr.length; // Determine whether the number of lines exceeds the number of lines we specify , if it exceeds, change the number of lines to the exceeded, because we need to calculate the string length
for (loop = 0; loop < lines; loop ) {//Loop to measure the total length of the string, not much to say
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)) {//Determine whether the string length exceeds the standard
field.value = value.substring(0, maxlimit);//Delete the excess string
}
}

function showOverWords(obj, maxlength) {//Display the remaining number of input characters obj is a txteara object, maxlength maximum length
len = obj.value.length;
$("#wordCount"). html(maxlength - len); //This sentence is from jquery, you can change it yourself. It means modifying the value of id to wordCount tag
}

function checkstr(str, digit) {// Determine whether the length of the 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) {//Anything greater than 255 is Chinese
n = 2;//If it is Chinese, it is 2 bytes
} else {
n = 1;//English, not much to say
}
}
if (n > digit) {
return true;
} else {
return false;
}
}


ok There are three functions above. We can achieve the functions we want by calling these three functions.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn