SIGN UP MEMBER LOGIN:    
ARTICLE

Character Limiting in TextBox in ASP.NET Using JQuery and Display Red When Reaches Out of Limit

Posted by Abhimanyu Kumar Vatsa Articles | JQuery February 08, 2012
In this article you are going to learn how to use jQuery to limit the length of characters in a TextBox and display the text in red when the limit is exceeded.
Reader Level:

Introduction

Look at the images given blow that you frequently see on websites. In this article you are going to learn hoe to do this.

image001.jpg
The screen when the character length exceeds the limit:

  image002.jpg
Follow the steps to create such a system.

Step 1:

Add the reference of the jQuery file in the head section of the web page, for example:

    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>

Step 2:

Add the following jQuery method in the head section of the web page:

    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            var txt = $("textarea[id$=txtComments]");
            var txtMsg = $("label[id$=txtStatus]");
            $(txt).keyup(function () {
                var length = $(txt).val().length;
                $(txtMsg).text("Total " + length + " Characters out of 200.");
                $(txtMsg).css("background-color", length >= 201 ? "#FF0000" : "#FFFFFF");
            });
        });
    </script>

Step 3:

Design the web page which contains TextBox, Label and a Button; for example:

    <div>
    Enter your comment
    <br />
    <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="164px" Width="298px"/>
    <br />
    <label id="txtStatus"></label>
    <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
    </div>

Now look at my complete coding.

Complete Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET & jQuery Post</title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function () {
            var txt = $("textarea[id$=txtComments]");
            var txtMsg = $("label[id$=txtStatus]");
            $(txt).keyup(function () {
                var length = $(txt).val().length;
                $(txtMsg).text("Total " + length + " Characters out of 200.");
                $(txtMsg).css("background-color", length >= 201 ? "#FF0000" : "#FFFFFF");
            });
        });
    </script>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    Enter your comment
    <br />
    <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="164px" Width="298px"/>
    <br />
    <label id="txtStatus"></label>
    <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
    </div>
    </form>
</body>
</
html>

I recommend that you to download the attached file and check it. I hope you like it. Thanks.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Abhimanyu. You have presented your article very nicely. Everything is clearly understood.

Posted by Adora Krause Feb 08, 2012

Good job done,I will try this.

Posted by Laura Parker Feb 08, 2012

Nice article, Abhimanyu. Thanks for sharing.

Posted by Abhi Kumar Feb 08, 2012

Regards for this article, thanks

Posted by Nitin Singh Feb 08, 2012
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor