Friday 1 February 2013

MaxLength in Multiline Textbox ASP.NET


MaxLength in Multiline Textbox ASP.NET


Well Yesterday I came across this question.. So I want to share the solution with all the Community Members. Special Thanks to VINZ for providing the nice solution:



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>MultiLine TextBox MaxLength</title>
   
        <script type="text/javascript" language="javascript">

        function ValidateLimit(obj, maxchar) {
            if (this.id) obj = this;
            var remaningChar = maxchar - obj.value.length;
            var lb = document.getElementById('<%= Label1.ClientID %>');
            lb.innerHTML = remaningChar;


            if (remaningChar <= 0) {
                obj.value = obj.value.substring(maxchar, 0);
                lb.innerHTML = "0";
            }
        }


    </script>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="ValidateLimit(this,140)">   </asp:TextBox> <br />
    Remaining char: <asp:Label ID="Label1" runat="server" Text="140"></asp:Label>

    </div>
    </form>
</body>
</html>

Apart from this here are few other alternatives:

Using Javascript
http://www.codeproject.com/Tips/82655/ASP-NET-Limit-number-of-characters-in-TextBox-cont

Using Jquery
http://www.dotnetcurry.com/ShowArticle.aspx?ID=396
http://www.aspsnippets.com/Articles/Limit-number-of-characters-in-an-ASPNet-Multiline-TextBox-using-jQuery.aspx

1 comment:

  1. I read this article, nice conversation about text.keep sharing more.

    ReplyDelete

Thank You for Your Comments. We will get back to you soon.

back to top