Copy codeThe Code is as follows:
<Script language = javascript>
<! --
String. prototype. len = function (){
Return this. replace (/[^ \ x00-\ xff]/g, "**"). length;
}
// Set maxlength for multiline TextBox
Function setMaxLength (object, length)
{
Var result = true;
Var controlid = document. selection. createRange (). parentElement (). id;
Var controlValue = document. selection. createRange (). text;
If (controlid = object. id & controlValue! = "")
{
Result = true;
}
Else if (object. value. len ()> = length)
{
Result = false;
}
If (window. event)
{
Window. event. returnValue = result;
Return result;
}
}
// Check maxlength for multiline TextBox when paste
Function limitPaste (object, length)
{
Var tempLength = 0;
If (document. selection)
{
If (document. selection. createRange (). parentElement (). id = object. id)
{
TempLength = document. selection. createRange (). text. len ();
}
}
Var tempValue = window. clipboardData. getData ("Text ");
TempLength = object. value. len () + tempValue. len ()-tempLength;
If (tempLength> length)
{
TempLength-= length;
// Alert (tempLength );
// Alert (tempValue );
Var tt = "";
For (var I = 0; I <tempValue. len ()-tempLength; I ++)
{
If (tt. len () <(tempValue. len ()-tempLength ))
Tt = tempValue. substr (0, I + 1 );
Else
Break;
}
TempValue = tt;
Window. clipboardData. setData ("Text", tempValue );
}
Window. event. returnValue = true;
}
// -->
</Script>
Set the textbox or textarea attributes of multiple rows.
Onkeypress = "javascript: setMaxLength (this, 100);" onpaste = "limitPaste (this, 100 )"
Now, we can automatically distinguish between Chinese and English. This solution is good for you to share.