Precisely control the number of Chinese and English characters in the input box

Source: Internet
Author: User

<Script language = "JavaScript" type = "text/JavaScript">
<! --

Function limitlength (value, bytelength, attribute)
{
VaR newvalue = value. Replace (/[^ \ x00-\ xFF]/g ,"**");
VaR length = newvalue. length;

// When the number of bytes entered is smaller than the set number of bytes
If (length * 1 <= bytelength * 1)
{
Return;
}
VaR limitdate = newvalue. substr (0, bytelength );
VaR COUNT = 0;
VaR limitvalue = "";
For (VAR I = 0; I <limitdate. length; I ++)
{
VaR flat = limitdate. substr (I, 1 );
If (flat = "*")
{
Count ++;
}
}
VaR size = 0;
VaR istar = newvalue. substr (bytelength * 1-1, 1); // check whether the checkpoint is "×"

// If the base point is X; determines whether X is an even number or an odd number within the base point.
If (count % 2 = 0)
{
// When the value is an even number
Size = count/2 + (bytelength * 1-count );
Limitvalue = value. substr (0, size );
}
Else
{
// When the value is an odd number
Size = (count-1)/2 + (bytelength * 1-count );

Limitvalue = value. substr (0, size );
}
Alert ("maximum input" + bytelength + "bytes (equivalent to" + bytelength/2 + "Chinese characters )! ");
Document. getelementbyid (attribute). value = limitvalue;
// Document. getelementbyid ("MSG"). innerhtml = bytelength;
Return;
}


// -->
</SCRIPT>

 

<Asp: textbox runat = "server" id = "txtcomment" Height = "77px"
Ontextchanged = "txtcomment_textchanged" textmode = "multiline" width = "307px"> </ASP: textbox>
<Textarea runat = "server" Cols = "50" rows = "10" name = "Explain" id = "Explain"> </textarea>

 

Add:

Explain. attributes ["onpropertychange"] = "limitlength (this. Value, 10, 'explain ')";

 

 

Complete page front-endCodeYes:

 

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<% @ Register Assembly = "ajaxcontroltoolkit" namespace = "ajaxcontroltoolkit" tagprefix = "PC3" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> dropdownlist </title>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function limitlength (value, bytelength, attribute)
{
VaR newvalue = value. Replace (/[^ \ x00-\ xFF]/g ,"**");
VaR length = newvalue. length;

// When the number of bytes entered is smaller than the set number of bytes
If (length * 1 <= bytelength * 1)
{
Return;
}
VaR limitdate = newvalue. substr (0, bytelength );
VaR COUNT = 0;
VaR limitvalue = "";
For (VAR I = 0; I <limitdate. length; I ++)
{
VaR flat = limitdate. substr (I, 1 );
If (flat = "*")
{
Count ++;
}
}
VaR size = 0;
VaR istar = newvalue. substr (bytelength * 1-1, 1); // check whether the checkpoint is "×"

// If the base point is X; determines whether X is an even number or an odd number within the base point.
If (count % 2 = 0)
{
// When the value is an even number
Size = count/2 + (bytelength * 1-count );
Limitvalue = value. substr (0, size );
}
Else
{
// When the value is an odd number
Size = (count-1)/2 + (bytelength * 1-count );

Limitvalue = value. substr (0, size );
}
Alert ("maximum input" + bytelength + "English (or" + bytelength/2 + "Chinese characters )! ");
Document. getelementbyid (attribute). value = limitvalue;
// Document. getelementbyid ("MSG"). innerhtml = bytelength;
Return;
}


// -->
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">

<Textarea runat = "server" Cols = "50" rows = "10" name = "Explain" id = "Explain"> </textarea>

</Form>

</Body>
</Html>

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.