Limit the Maximum number of Characters in the texbox

Posted by Venkat | Labels: , ,

Here is the code how to limit the no.of charater entered by the user in TextBox , mostly this can be implemented on Post comment , Feedback , Message board ...



This example demonstrates on how are we going to limit the number of characters to be typed into the TextBox using JavaScript and display the remaining characters in a Label Control.


<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);
alert('Character Limit exceeds!');
return false;

}
else
{return true;}
}

</script>

<body runat="server">
<form id="form1" runat="server">

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="validatelimit(this,300)"> </asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="0"></asp:Label>

</form>

</body>
Note: 300 is the MAX length of the characters in the TextBox..Just change the value of 300 based on your requirements..

0 comments:

Post a Comment

Thanks for the Comments.

PayOffers.in