Limit Characters in a Textarea with Prototype
- Simple Hide and Show with Prototype
- Event Observe and Event Listeners with Prototype
- JavaScript Script.aculo.us Fade with a Timer
- Evolving Styles: Add, Change Remove Classes with Prototype
- First, Last, Active and Only List Styles With Prototype
- Alternating Colors Using Prototype
- Limit Characters in a Textarea with Prototype
- Zip Code, Phone Number and Replacing Empty Fields with Really Easy Validation
- Change Rel External to Target Blank using Prototype
- Open Links to a Different Site in a New Window with Prototype
Lately I have fallen in love with prototype and in effort to challenge myself and my skills I continue to brainstorm ways to use Prototype as often as possible.
Until I started using the Really Easy Validation library I hadn't done a lot with Prototype and forms. But after I did some custom work using that library I realized I also needed a text area with a character counter. That was easy enough, but I wanted to make it something that was nice and expandable, so I did.
We'll start with the HTML code I already had for the two text areas whose character's I want to count and limit.
HTML
<div class="formrow">
<label for="txtdesc">Description</label> <span>(<span id="counter-txtdesc"><b>#</b></span> characters)</span><br />
<textarea name="txtdesc" id="txtdesc"></textarea>
</div>
<div class="formrow">
<label for="comments">Comments</label><br />
<textarea name="comments" id="comments"></textarea>
</div>
I will change two lines in my .js file to initiate the character counting.
JavaScript
function init(){
makeItCount('txtdesc', 300, false);
makeItCount('comments',100);
}
I've defined that both text areas: txtdesc and comments are to have limits. The text area description will be limited to 300 characters and comments will be limited to 100 characters.
New 3rd, optional, parameter Added 4/16/08
The 3rd parameter allows you to override the limiting functionality. It defaults to true but can be overridden by passing false this will allow the text field to accept and not trim down the entry to the maximum amount, ti will continue to count up and inform you how over the limit you are so you can pick and choose what to trim down.
Is that more twitter like? I wouldn't know since I don't twitter(yet).
Since an element with the id 'counter-txtdesc' exists the script has a place to put the character count, in this case: 0/300.
No element with the id 'counter-comments' exists, so the script will create a div immediately below the text area with that id and puts 0/100 as the text within it.
When the count is below the maximum number of characters the relative counter- element also has the class of charcount-safe applied to it. When you meet or go over the character limit charcount-safe is removed and charcount-limit is applied. The script also trims down your content to the specified number of characters once you have gone over it.
If either text area doesn't exist the script does not return an error, it just disregards the assignment to count that area.
Pretty easy. There's additional customization I'd like to add to it but I need to research a few things before I do that!
Version 2
New update by request.
I've added a 3rd parameter (optional) to override the limiting functionality. Documentation above has been altered to allow for this change.
Demo & Download
You can view the demo to see this example live or download the script under the MIT License, same as Prototype and Scriptaculous.
Update 4/17/08
In response to a comment about an IE bug I looked into the problem...the problem was with the demo, not the script. I had used a meta tag <meta name="description" value="" /> and named the text area description. IE was picking up the <meta> tag instead of the <textarea> tag. Which I guess is just a friendly reminder that:
nameis technically deprecated according to the w3c's XHTML 1 documentation- Just be unique - Just name and id elements uniquely, only duplicate the name and id within the same element to eliminate confusion, k?
Old versions
- Version 2.1 - Released 4/16/2008 - Added documentation in script 5/20/2008
- Version 1.1 - Released 1/17/2008 - Added documentation in script 5/20/2008
Popularity: 76% [?]
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)



Hi, Great article, I was looking for something like that for a while :).
I'm developing a web application for sending SMS messages, with this application you can send English messages (max characters per message is 160) but if you want to send a message with complicated characters (Chinese, Arabic, ... etc), the max number of characters becomes 70 !!! I'm wondering how can the script be modified to do this? When you type a letter in Chinese for example the limit changes instantly to 70 ! The problem is that I don't know Javascript I'm wondering whether hacking this script to do this is easy or not.
Thanks :)