A problem with JS to limit the number of bytes entered by the user.

Source: Internet
Author: User
Tags count delete key functions implement key net return version
js| problem

There is a textarea on the page (note that multiple lines), now need to use JS to implement the following functions.

1. You can enter only a certain number of bytes of characters, such as the maximum allowable length is 10, you can only enter 10 letters or 5 Chinese characters (double-byte characters).
2. It also allows the user to use the BACKSPACE key, delete key, and other non character keys.
3. IE and Firefox must be supported at the same time, too low version can not be considered.
4. When the user pastes the time also must guarantee does not exceed the allowable maximum length, cannot consider simply uses the keyboard input.
5. The above function is byte based, not character.

Did not fix for a while, there is no particularly good information on the Internet.
If you have a brother to help me out, I can put a non technical points or donate the available points.
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 field length of alert (fielddesc+) 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= "onkeyup=" checkfieldlength (' Testarea ', ' more than the number of characters allowed to enter ', 20 ); "></TEXTAREA>
<div id= "TestInfo" > has been entered: <span id= "ShowMsg" ></span></div>
</FORM>
</BODY>
</HTML>

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

The customer must be? You can catch those few events. Judge the number of characters in an event
About Chinese and English, you can refer to
Http://blog.csdn.net/net_lover/archive/2002/03/29/6898.aspx
---------------------------------------------------------------

<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; Get single byte count
var s = v.length-d; Get two-byte count

var Alllen = d + S * 2;

if (O.getattribute && Alllen >nmaxlen) {
O.value=o.value.substring (0,NMAXLEN/2)
}
}
</Script>
<body>
<input typ= "text" onkeyup= "return Ismaxlen (This)" id= "Txtcomment" maxlength= "" textmode= "MultiLine" height= "90px "/>
</body>

There is a textarea on the page (note that multiple lines), now need to use JS to implement the following functions.

1. You can enter only a certain number of bytes of characters, such as the maximum allowable length is 10, you can only enter 10 letters or 5 Chinese characters (double-byte characters).
2. It also allows the user to use the BACKSPACE key, delete key, and other non character keys.
3. IE and Firefox must be supported at the same time, too low version can not be considered.
4. When the user pastes the time also must guarantee does not exceed the allowable maximum length, cannot consider simply uses the keyboard input.
5. The above function is byte based, not character.

Did not fix for a while, there is no particularly good information on the Internet.
If you have a brother to help me out, I can put a non technical points or donate the available points.
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 field length of alert (fielddesc+) 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= "onkeyup=" checkfieldlength (' Testarea ', ' more than the number of characters allowed to enter ', 20 ); "></TEXTAREA>
<div id= "TestInfo" > has been entered: <span id= "ShowMsg" ></span></div>
</FORM>
</BODY>
</HTML>

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

The customer must be? You can catch those few events. Judge the number of characters in an event
About Chinese and English, you can refer to
Http://blog.csdn.net/net_lover/archive/2002/03/29/6898.aspx
---------------------------------------------------------------

<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; Get single byte count
var s = v.length-d; Get two-byte count

var Alllen = d + S * 2;

if (O.getattribute && Alllen >nmaxlen) {
O.value=o.value.substring (0,NMAXLEN/2)
}
}
</Script>
<body>
<input typ= "text" onkeyup= "return Ismaxlen (This)" id= "Txtcomment" maxlength= "" textmode= "MultiLine" height= "90px "/>
</body>



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.