ASP.NET Articles

Setting MaxLength of TextArea and Mutli-Line Textbox

MaxLength property doesn’t work for multi-line text box and text area in ASP.Net. Using the JavaScript code given below, we can easily set the max length for multi-line text box or text area.

<html>
<head>
<script>
function restrict(obj,size)
{
var key = window.event.keyCode;
if(!(
key==8 //backspace
|| key==46 //delete
|| key==37 //left
|| key==38 //up
|| key==39 //right
|| key==40 //down
)&&(obj.value.length>=size))
window.event.returnValue = false;
}
function trim(obj,size)
{
obj.value = obj.value.substring(0,size);
}
</script>
</head>
<body>
<form name=”frm”>
<textarea id=”txt” onkeydown=”restrict(this,10);” onchange=”trim(this,10);”></textarea>
</form>
</body>
</html>

Categories: ASP.NET Articles

Leave a Reply