One problem is that JavaScript is used to limit the number of user input bytes.

Source: Internet
Author: User
Tags delete key

Http://blog.csdn.net/jackeyabc/archive/2007/04/11/1561117.aspx

 

Http://topic.csdn.net/u/20070316/14/6A6D551E-1C7B-4A9E-A2AC-64E15E501AD3.html

 

There is a textarea on the page (note that there are multiple rows). Now we need to use js to implement the following functions.

1. You can only enter characters of a certain byte. For example, if the maximum length is 10, you can only enter 10 letters or 5 Chinese characters (double-byte characters ).
2. You can also use the backspace key, delete key, and other non-character keys.
3. Both IE and Firefox must be supported. if the version is too low, skip this step.
4. during pasting, you must ensure that the maximum length is not exceeded.
5. the above functions are based on bytes, not characters.

I haven't done it for a long time, and I don't have any good information on the Internet.
If a brother helps me solve the problem, I can place non-technical scores or donate available scores.
Thank you!

---------------------------------------------------------------

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Script language = "JavaScript">
<! --
Function cutstrlength (STR, ilength)
{
VaR TMP = 0;
VaR Len = 0;
VaR oklen = 0
For (VAR I = 0; I <ilength; I ++)
{
If (Str. charcodeat (I)> 255)
TMP + = 2
Else
Len + = 1
Oklen + = 1
If (TMP + Len = ilength)
{
Return (Str. substring (0, oklen ));
Break;
}
If (TMP + Len> ilength)
{
Return (Str. substring (0, oklen-1) + "");
Break;
}
}
}

Function checkfieldlength (fieldname, fielddesc, fieldlength)
{

VaR STR = Document. getelementbyid (fieldname). value;
VaR thelen = 0;
VaR teststr = '';
For (I = 0; I <Str. length; I ++)
{
Teststr = Str. charat (I );
If (Str. charcodeat (I)> 255)
Thelen = thelen + 2;
Else
Thelen = thelen + 1;
}
Document. getelementbyid ('showmsg '). innertext = thelen;
If (thelen> fieldlength)
{
Document. getelementbyid ('showmsg '). innertext = fielddesc;
// The length of the alert (fielddesc + "field exceeds the specified length! ");
// Document. getelementbyid (fieldname). Focus ();
Document. getelementbyid (fieldname). value = cutstrlength (STR, fieldlength );
Return false;
}
Else
{
Return true;
}
}
// -->
</SCRIPT>
</Head>

<Body>
<Form method = post action = "">
<Textarea id = "testarea" name = "testarea" rows = "3" Cols = "22" onkeyup = "checkfieldlength ('testarea ', 'exceeds the allowed number of characters ', 20); "onchange =" checkfieldlength ('testarea ', 'exceeds the allowed number of characters', 20); "> </textarea>
<Div id = "testinfo"> already entered: <span id = "showmsg"> </span> </div>
</Form>
</Body>
</Html>

---------------------------------------------------------------

Must customers? You just need to capture those events. Determine the number of characters in an event
For more information about Chinese and English, see
Http://blog.csdn.net/net_lover/archive/2002/03/29/6898.aspx
---------------------------------------------------------------

<HTML>
<Head>
<Script language = "JavaScript" type = "text/JavaScript">
Function ismaxlen (o ){
VaR nmaxlen = O. getattribute? Parseint (O. getattribute ("maxlength ")):"";

VaR v = O. value;
VaR d = (V. Match (/[/x00-/xFF]/G) | ""). length; // obtain the number of single bytes
VaR S = V. Length-D; // obtain the number of Double Bytes

VaR alllen = d + S * 2;

If (O. getattribute & alllen> nmaxlen ){
O. value = O. value. substring (0, nmaxlen/2)
}
}
</SCRIPT>
</Head>
<Body>
<Input typ = "text" onkeyup = "Return ismaxlen (this)" id = "txtcomment" maxlength = "10" textmode = "multiline" Height = "90px"/>
</Body>
</Html>

There is a textarea on the page (note that there are multiple rows). Now we need to use js to implement the following functions.

1. You can only enter characters of a certain byte. For example, if the maximum length is 10, you can only enter 10 letters or 5 Chinese characters (double-byte characters ).
2. You can also use the backspace key, delete key, and other non-character keys.
3. Both IE and Firefox must be supported. if the version is too low, skip this step.
4. during pasting, you must ensure that the maximum length is not exceeded.
5. the above functions are based on bytes, not characters.

I haven't done it for a long time, and I don't have any good information on the Internet.
If a brother helps me solve the problem, I can place non-technical scores or donate available scores.
Thank you!

---------------------------------------------------------------

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Script language = "JavaScript">
<! --
Function cutstrlength (STR, ilength)
{
VaR TMP = 0;
VaR Len = 0;
VaR oklen = 0
For (VAR I = 0; I <ilength; I ++)
{
If (Str. charcodeat (I)> 255)
TMP + = 2
Else
Len + = 1
Oklen + = 1
If (TMP + Len = ilength)
{
Return (Str. substring (0, oklen ));
Break;
}
If (TMP + Len> ilength)
{
Return (Str. substring (0, oklen-1) + "");
Break;
}
}
}

Function checkfieldlength (fieldname, fielddesc, fieldlength)
{

VaR STR = Document. getelementbyid (fieldname). value;
VaR thelen = 0;
VaR teststr = '';
For (I = 0; I <Str. length; I ++)
{
Teststr = Str. charat (I );
If (Str. charcodeat (I)> 255)
Thelen = thelen + 2;
Else
Thelen = thelen + 1;
}
Document. getelementbyid ('showmsg '). innertext = thelen;
If (thelen> fieldlength)
{
Document. getelementbyid ('showmsg '). innertext = fielddesc;
// The length of the alert (fielddesc + "field exceeds the specified length! ");
// Document. getelementbyid (fieldname). Focus ();
Document. getelementbyid (fieldname). value = cutstrlength (STR, fieldlength );
Return false;
}
Else
{
Return true;
}
}
// -->
</SCRIPT>
</Head>

<Body>
<Form method = post action = "">
<Textarea id = "testarea" name = "testarea" rows = "3" Cols = "22" onkeyup = "checkfieldlength ('testarea ', 'exceeds the allowed number of characters ', 20); "onchange =" checkfieldlength ('testarea ', 'exceeds the allowed number of characters', 20); "> </textarea>
<Div id = "testinfo"> already entered: <span id = "showmsg"> </span> </div>
</Form>
</Body>
</Html>

---------------------------------------------------------------

Must customers? You just need to capture those events. Determine the number of characters in an event
For more information about Chinese and English, see
Http://blog.csdn.net/net_lover/archive/2002/03/29/6898.aspx
---------------------------------------------------------------

<HTML>
<Head>
<Script language = "JavaScript" type = "text/JavaScript">
Function ismaxlen (o ){
VaR nmaxlen = O. getattribute? Parseint (O. getattribute ("maxlength ")):"";

VaR v = O. value;
VaR d = (V. Match (/[/x00-/xFF]/G) | ""). length; // obtain the number of single bytes
VaR S = V. Length-D; // obtain the number of Double Bytes

VaR alllen = d + S * 2;

If (O. getattribute & alllen> nmaxlen ){
O. value = O. value. substring (0, nmaxlen/2)
}
}
</SCRIPT>
</Head>
<Body>
<Input typ = "text" onkeyup = "Return ismaxlen (this)" id = "txtcomment" maxlength = "10" textmode = "multiline" Height = "90px"/>
</Body>
</Html>

 

 

 

 

Statistics determine the number of bytes input by the current user to distinguish Chinese characters and letters

Http://www.okajax.com/a/200812/1222S192008.html

 

 

 

Sometimes this problem occurs in the system: allow the user to enter a certain content, only five or ten characters can be entered. In this case, we simply use "document. cnwebform. title. value. length "cannot determine Chinese characters, but can only be determined by the number of bytes. As follows :/**
* Count the number of string bytes
*
* Return integer
*/
String. Prototype. bytecount = function ()
{
TXT = This. Replace (/(<. *?>) /Ig ,'');
TXT = TXT. Replace (/([u0391-uFFE5])/ig, '11 ');
VaR COUNT = TXT. length;
Return count;
} With the above function, we can calculate the number of bytes entered by the user. Call method: var COUNT = document. cnwebform. title. value. bytecount (); count is the number of bytes entered by the user. Because Chinese characters occupy two bytes, we can calculate the number of Chinese characters and characters: if (count <10 | count> 20 ){
Alert ("the number of words is limited to 5 ~ 10 Chinese characters, or 10 ~ 20 English characters. ");}
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.