Live Demos

Wednesday, May 05, 2010

Character Count with jQuery and ASP.NET Web Forms




I recently needed to add a character count on several textareas on a Web app and decided to use a jQuery plugin. I know what you’re thinking, “I can’t believe Deran would use a jQuery plugin…I thought he hated jQuery”…or maybe you thought, “Of course you’re going to use a plugin…you always use a plugin”. Either way, I thought I’d share what I ended up doing.

So, basically, I have several <asp:textbox textmode=”multiline”/> objects on my page and I wanted a way to append a character count. Also you should note that my textboxes were embedded in a ContentPlaceHolder within a master page.

I ended up finding this jQuery plugin called NobleCount by The Product Guy (Jeremy Damian Horn). It’s a pretty nice plugin with several features and it plays nice with CSS. The only problem I found was that all his examples were for setting the character count on one textarea. I didn’t want to have all that redundant code, so I ended up writing something that loops through each textarea and finds the matching character count area. Remember I’m using the master page and .NET 3.5, so my ASP.NET ID won’t match the client ID. Here’s what I came up with:

$(document).ready(function () {

$(
'textarea').each(function () {
var id = $(this).attr("id");
var lastTwoCharInId = id.substring(id.length - 2, id.length);

$(
this).NobleCount('#count' + lastTwoCharInId, { on_negative: 'goRed', max_chars: 500 });
});

});

So what this says is…

  1. Loop through all textareas
  2. Get the id of the textarea
  3. Get the last two characters of the id (NOTE: I specifically named my ids so I could pull an identifier from them. I used two digits because it is HIGHLY unlikely that I’d have more than 99 textareas on the same page. Also, appending an identifier to the end of your textboxes allows jQuery to easily be able to take advantage of them with all the different selectors they have to offer.)
  4. Finally I call the NobleCount plugin and set my count span, the class of when the user goes into negative remaining, and I set the max characters.

You can also set max_chars to be $(this).attr(“maxlength), but that attribute is only rendered with textmode=SingleLine in ASP.NET. If you wanted to set different values, you could add a class with the max number and you’d have two selectors or set the max_chars to that class value. There are quite a few methods you could use.

This is why my HTML looks like:

    <asp:TextBox ID="tbQues01" runat="server" TextMode="MultiLine" MaxLength="500" Width="500px" />
<
div class="charbox"><span id="count01"></span> Characters Remaining</div>

My style looks like this:

.goRed {background: red; color: #fff}
.charbox {border: solid 1px #ccc; background: #e7e7e7; padding: 3px; width: 500px}

And of course this all generates these views:

image

image

I also wanted to note that Jeremy’s examples on the different settings are really good, so I would definitely check them out.

Thanks for reading!

Shout it

5 comments:

  1. The 80/20 rule refers to food proper? Believed it had been 90 thoroughly clean and 10 junk, maybe that is been altered due to the further energy
    you get from junk meals? I swear everyone eats as well little.


    Also visit my website ... mike chang monster mass youtube

    ReplyDelete
  2. k im chinese, despite the fact that i've been in America for almost my complete daily life so I NEEED Fb! I can't feel that the second i visit china,
    Fb AND YOUTUBE GETS BLOCKED! How am I gonna survive?

    PS, VPN=Laaaaaaaaaaaaaaaaaag

    my blog post free dating tips

    ReplyDelete
  3. Luke, just a handful of a lot more facts my good
    man.

    My blog - watch superman stamina free online

    ReplyDelete
  4. bis, may possibly not do it on delts and not certain about back, see tomorrow.
    My layout is according to loosely Powerbuilding, the Muscle
    mass and Brawn Way but with some extra quantity. 4-5 for your huge teams and 2-3 for your smaller.
    and traps with the

    Here is my blog six pack shortcuts training plan

    ReplyDelete
  5. Hey there! I just want to offer you a huge thumbs up for the excellent info you have got
    right here on this post. I'll be coming back to your site for more soon.

    Here is my page; Basyx Chair review

    ReplyDelete