SoFunction
Updated on 2025-03-01

JS code that limits the length of input characters in textbox or textarea


<script language=javascript>
<!--

=function(){
return (/[^\x00-\xff]/g,"**").length;
}

//Set maxlength for multiline TextBox
function setMaxLength(object,length)
{
var result = true;
var controlid = ().parentElement().id;
var controlValue = ().text;
if (controlid == && controlValue != "")
{
result = true;
}
else if (() >= length)
{
result = false;
}
if ()
{
= result;
return result;
}
}

//Check maxlength for multiline TextBox when paste
function limitPaste(object,length)
{
var tempLength = 0;
if()
{
if(().parentElement().id == )
{
tempLength = ().();
}
}
var tempValue = ("Text");
tempLength = () + () - tempLength;
if (tempLength > length)
{
tempLength -= length;
//alert(tempLength);
//alert(tempValue);
var tt="";
for(var i=0;i<()-tempLength;i++)
{
if(()<(()-tempLength))
tt=(0,i+1);
else
break;
}
tempValue=tt;
("Text", tempValue);
}

= true;
}

//-->
</script>