ARTICLE

TextBox maxlength in ASP. NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 28, 2011
Here we will see that how we can turn a TextBox font red after max length characters have been inserted, and also we check number of character has been inserted in the TextBox.
 
Reader Level:

Here we will see that how we can turn a TextBox font red after maxlength characters have been inserted, and also we check number of character has been inserted in the TextBox. But if we go back to a black font color if they were to delete back to less than maxlength.

Drag and drop a TextBox on the page. Select TextBox and press F4 to property window.

TextMode="MultiLine"

Now add the following code in the Head section of the .aspx page.

<head runat="server">

   <title></title>

   <style type="text/css">

       .validentry

        {

           background-color:#FFFFFF;

           color: #000000;

        }

       

       .invalidentry

        {

           background-color:#FFCCCC;

           color: #ff0000;

        }

   </style>

 

   <script language="javascript">

       function textCounter1(field, maxlimit) {

           if (field.value.length > maxlimit) {

                field.value = field.value.substring(0, maxlimit);

                document.getElementById('message').className = "invalidentry";

                document.getElementById('message').focus();

               //alert('You have reached your maximum character limit.');

            }

           else {

                document.getElementById('message').className = "validentry";

            }

          

        }

  

           function textCounter(field, countfield, maxlimit) {

               if (field.value.length > maxlimit)// if too long...trim it!

                    field.value = field.value.substring(0, maxlimit);

               else

                    countfield.value = maxlimit - field.value.length;

            }

   </script>

</head>

 

Now add the following code in the body section.

 

 

 

<body bgcolor="#FFFFFF">

<form name="blah" action="" method="post" runat="server">

   &nbsp;<asp:TextBox ID="message" runat="server" name="message" onkeydown="textCounter(this.form.message,this.form.remLen,120)"

       onkeyup="textCounter1(this.form.message, 120)" class="validentry"

       Height="101px" TextMode="MultiLine" Width="228px"></asp:TextBox>

   <br />

   <input readonly type="text" name="remLen" size="3" maxlength="3" value="120"

   position: absolute; style="width: 34px">Character left<br />

   <br />

   <br />

   </form>

</body>

Now run the application.

txtbox1.gif

Figure1

Now check number of character has been inserted in the TextBox.

txtbox4.gif

Figure2 

Now we can turn a TextBox font red after max length characters have been inserted in TextBox.

txtbox3.gif

Figure3

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Diagram
Become a Sponsor