On Time Character Counter for TextBox Control using JQuery in ASP.NET

No.of Views1691
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  25 Dec 2010 02:12:54
Tag : JQuery , HTML and CSS
Today I have introduced the new features for CodeGain article submit page to count characters for description textbox with JQuery on time. The character counter is support to users to display how many characters have entered and how many chars remaining in live.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Today I have introduced the new features for CodeGain article submit page to count characters for description textbox with JQuery in on time. The character counter is support to users to display how many characters they have entered and how many chars remaining. So I would like to share this with you.

Note: I have not use any external plug-ins for this implementation.

Implementation

Step 1

As usual create web application using visual studio with one web page. 

Image Loading

Step 2

Design page with one textbox and a div for display character count as message. The web Page looks like as below,

Image Loading

HTML Code

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

<!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>OnTime Character Counter for TextBox Control using JQuery in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="main" style="display: block;">
        <dvi>Welcome to Online character counter.</dvi>
        <div id="result">
        </div>
        <div>
            <asp:TextBox ID="txtMessage" TextMode="MultiLine" MaxLength="300" Width="400px" Height="100px"
                runat="server"></asp:TextBox>
        </div>
    </div>
    </form>
</body>
</html>

Step 3

And also create simple style sheet for make look and feel is good.

CSS

<style type="text/css">
        #main
        {
            display: block;
        }
        #result
        {
            font: normal normal bold 10px verdana;
        }
    </style>

Step 4

In order work with JQuery, you have to add JQuery Library from your local folder or from Google CDN. I always prefer use from Google CSN.

<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Step 5

Now start to write JavaScript code for count chars and display in div control as message for users.   
JavaScript Code

<script language="javascript" type="text/javascript">
        $(document).ready(function() {
// declare default character count 
           var maxchar = 300;
// set initial remaining character is acceptable by text box.
            $('#result').html("Remaining chars :" + maxchar);
// invoke key up event for txtMessage Control
            
$('#txtMessage').keyup(function() {
                // get each time length and display in div
                var txtmessage = $('#txtMessage').val();
                var remain = maxchar - txtmessage.length;
                if (remain >= 0) {
                    $('#result').html("Remaining chars :" + remain);
                }
                else {
                    $('#result').html("Remaining chars :0");
                    $('#txtMessage').val(txtmessage.substring(0, maxchar));
                }
            });
        });
    
    </script>

Code Explanation

1.    Declare Initial character count is 300
2.    And then set default acceptable chars to message textbox.
3.    The third line is important for invoke the KeyUp event for TextBox control in order to access chars length and etc

$('#txtMessage').keyup(function() {
});

4.    In the next line get entered characters and then in next line subtract from maximum acceptable count

var txtmessage = $('#txtMessage').val();
var remain = maxchar - txtmessage.length;

5.    In the next line check if remaining count is great than zero then set result how many remains count to div.

if (remain >= 0) 
{
$('#result').html("Remaining chars :" + remain);
}

6.    Otherwise set it remains is chars count is 0 and substring what is maximum characters is entered

$('#result').html("Remaining chars :0");   
$('#txtMessage').val(txtmessage.substring(0, maxchar));

Note: In your project you may need to other action if users entered maximum characters. Then you have to change else block logic as like you wanted.

Step 6

The code is ready now run application and enter words and see. Output as like below.
Output 

Image Loading

Note: for the Live demonstration I have set maximum acceptable count is 10

I have given live demonstration, try this features in on live below,

Live Demo

On Time Character Counter

Download Sample Project

Download source files -3 kb

Conclusion

In this article you have learned how to count characters on time in text box using JQuery in ASP.NET. This is most useful when you are allow users to enter comments for your articles. Thank for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
    The JQuery is powerful client side JavaScript library. Today I’m going to show you how to create cascading DropDownList using JQuery with JSON in ASP.NET.I hope you are all heard about cascading DropDownList and how to use with AJAX tool kit
    Published Date : 07/Dec/2010
    In this article I will demonstration to how to validate whether to check at least one checkbox is checked in the Gridview in ASP.NET using JQuery. The JQuery is significant library to manipulate client html and css with less line of code.Before Jquery come up we have used the pure JavaScript to implement this features. I will give the JavaScript code as well in end of this article.
    Published Date : 30/Dec/2010
    Today I have implemented to my project to check and uncheck all checkboxes within gridview in asp.net. So in this article we focus how to implement this features using JQuery with less code. And also I have given live demonstration as well you can try it in live.
    Published Date : 29/Dec/2010
    In this article, I will explain how to sort custom objects array using JQuery. Let’s say you have array with custom objects, the custom object has few attributes as well, so in this case, you may need to sort objects with different order with specific attributes.
    Published Date : 12/Jan/2011
    The JQuery is powerful library to manipulate client side HTML, CSS, object and etc. On this article I will explain how to sort element within array using JQuery. In this demonstration I will show sort for string and numeric elements.
    Published Date : 11/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top