Monday, March 9, 2009

Restrict length of multiline textbox in asp.net

function checkMaxLength( txt,e, length )
{

var mLen = txt[ " MaxLength " ];
if(null == mLen)
mLen = length;

var maxLength = parseInt ( mLen );
if(!isSpecialCharacters( e ))
{
if(txt.value.length > maxLength-1)
{
if(window.event)//for Internet Explorer
e.returnValue = false;
else// For Firefox
e.preventDefault();
}
}
}

function isSpecialCharacters(e)
{
if(e.keyCode !=8 && e.keyCode!=46 && e.keyCode!=37 && e.keyCode!=38 && e.keyCode!=39 && e.keyCode!=40)
return false;
else
return true;
}

add this in code behind file of the control

onkeyDown="checkMaxLength( this ,event ,'100' );"

No comments: