Text Box Characters Counter


In almost all the web projects, we require a multiline TextBox control. It was annoying to find that maxLength property doesn’t work on multiline textboxes.

There are various ways to solve this issue, here i am using javascript to restrict user once he reach maxlimit.

<script type=”text/javascript” language=”javascript”>

function validatelimit(obj, maxchar)
{

if(this.id) obj = this;

var remaningChar = maxchar – obj.value.length;
document.getElementById(‘<%= Label1.ClientID %>’).innerHTML = remaningChar;

if( remaningChar <= 0)
{
obj.value = obj.value.substring(maxchar,0);
return false;

}
else
{return true;}

}

</script>

<asp:TextBox ID=”TextBox1″ runat=”server” TextMode=”MultiLine” onkeyup=”return validatelimit(this,500)” Height=”400px”></asp:TextBox>
<asp:Label ID=”Label1″ runat=”server” Text=”500″></asp:Label>

Other useful links :

http://forums.asp.net/t/1251514.aspx

Hope this will Help u.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s