Js controls the number of input strings in textarea. Press the mouse and hold it up to determine the number of input characters. jstextarea

Source: Internet
Author: User

Js controls the number of input strings in textarea. Press the mouse and hold it up to determine the number of input characters. jstextarea

[Html code]

<Table>

<Tr>

<Td width = "150"> text message content: </td>

<Td>

<Textarea name = "message" cols = "96" rows = "5" onKeyDown = "textCounter (message, remLen, 65 );"

OnKeyUp = "textCounter (message, remLen, 65);"> </textarea>

<Td>

</Tr>

<Tr> <td> </td>

<Td> You can also enter <input name = "remLen" type = "text" value = "65" size = "5" readonly = "readonly"> characters, each text message can contain up to <strong> 65 </strong> characters </td>

</Tr>

</Table>


[Corresponding js Code]

<Script>
Function textCounter (field, countfield, maxlimit ){
If (field. value. length> maxlimit)
Field. value = field. value. substring (0, maxlimit );
Else
Countfield. value = maxlimit-field. value. length;
}
</Script>


Js question: How does one specify the number of characters in a string?

Check whether this meets your requirements.

Public class Test {

Public static void main (String [] args ){
Test t = new Test ();
String s = "wohoeniohoaoohah ";
String c = "o ";
Int x = t. getNum (s, c );
System. out. println (x );
}
Public int getNum (String s, String c) {// s is the original String. c is the String we are looking for. Of course, it can be a character.
Int x = s. indexOf (c); // checks whether C exists in S! If x =-1, it indicates no.
Int sum = 0; // number of occurrences!
While (x! =-1 ){
Sum ++;
S = s. replaceFirst (c, ""); // replace C with "". recyclically find the next C
X = s. indexOf (c );
}
Return sum;
}
}

How can I use JavaScript to limit the number of characters in multiple lines of text fields? to limit the number of characters in a single line of text box, it cannot appear again after entering the specified number of characters

In this way:
<Textarea oninput = "s ()" onpropertychange = "s ()"> </textarea>
Function s (){
Var leng = number of characters to limit
If (this. value. lengtg> = leng ){
// Add a limit to achieve stronger results
Event. returnValue = false; // when the number of words reaches the specified value, any buttons are invalid.
// Truncate the string
This. value = this. value. substring (0, leng );
}
}

Because onpropertychange is only valid in IE, and oninput is valid in most browsers, the two are used together.

Related Article

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.