Interactive character limit for textarea using Jquery
Jquery, how-to, javascript, tutorial November 9th, 2007For a textbox, character of a field can easily be limited with maxlength attribute. But maxlength doesn’t work for a textarea. So, It needs an alternate way. For one of my project, I wrote a function for this purpose where I used jQuery. Here I am explaining how I did it.
Let’s look an example of this interactive solution here.
OK. Now we will make this in 2 easy steps.
1. Import your jQuery file and write the function "limitChars(textid, limit, infodiv)" in the head section of your page.
This function takes 3 parameters. They are:
- textid : (string) The ID of your textarea.
- limit : (num) The number of character you allow to write.
- infodiv : (string) The ID of a div, in which limit information will be shown .
Here is the function:
1: <script language="javascript" src="Jquery.js"></script>
2: <script language="javascript">
3: function limitChars(textid, limit, infodiv)
4: {
5: var text = $('#'+textid).val();
6: var textlength = text.length;
7: if(textlength > limit)
8: {
9: $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
10: $('#'+textid).val(text.substr(0,limit));
11: return false;
12: }
13: else
14: {
15: $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
16: return true;
17: }
18: }
19: </script>
Remember, the script is using jQuery. So be careful about the path of jquery at first line of this script.
2. Bind the function to the keyup event of your textarea. Do this in jQuery’s ready event of document. Just like this:
1: $(function(){
2: $('#comment').keyup(function(){
3: limitChars('comment', 20, 'charlimitinfo');
4: })
5: });
Here my textarea’s id is ‘comment’ , limit of characters is 20 and limit information will shown in a div whose id is ‘charlimitinfo’. That’s all, we have made an "Interactive character limiter" for our textarea using Jquery.
If you are not using jQuery for your site, it will not be wise to include it only for this script. You can get the same functionality from javascript without jquery. Look at this example. It looks like and works 100% as the previous though not using jquery.
Here you need just 2 changes. First, change the function "limitChars" as follows :
1: function limitChars(textarea, limit, infodiv)
2: {
3: var text = textarea.value;
4: var textlength = text.length;
5: var info = document.getElementById(infodiv);
6:
7: if(textlength > limit)
8: {
9: info.innerHTML = 'You cannot write more then '+limit+' characters!';
10: textarea.value = text.substr(0,limit);
11: return false;
12: }
13: else
14: {
15: info.innerHTML = 'You have '+ (limit - textlength) +' characters left.';
16: return true;
17: }
18: }
And finally, call the function manually on keyup event of your textarea. Use "this" keyword for textarea instead of id when calling the function. example:
1: <textarea name="comment" id="comment" onkeyup="limitChars(this, 20, 'charlimitinfo')">
2: </textarea>
Finished. We made the solution again without dependency of any javascript framework.
Please let me know if you have an idea to make it better.













November 18th, 2007 at 10:36 pm
Nice Job done. But why prototype is not given?
November 18th, 2007 at 10:50 pm
If you are using prototype for your site, you can use the second function. It doesn’t depends on jQuery.
November 19th, 2007 at 7:03 am
The only full proof way to do this is actually with a timer that checks the length of the input in intervals. If I type more than 20 characters in notepad, then copy it, and use Edit >> Paste in the browser window, it will allow more than 20 for this example.
November 19th, 2007 at 3:39 pm
Hi man,
Can you send the source of this template to my email address ? I like it too much.
Thx.
November 22nd, 2007 at 5:36 am
@Brian Reindel:
Hi friend,
I think, javascript cannot make anything “full proof” when a user wants to hack. Because, most of browsers allow a user to disable javascript. So, front end validations just alert a user about invalid inputs on a pre-submission stage. Sensitive issues must validate again from back end.
Thanx a lot.
November 22nd, 2007 at 5:44 am
@paulo :
Hi,
Thanks for your interest. There is no any server script uses in this demo. So, you can easily get the source from browsers “view source” option.
November 22nd, 2007 at 6:23 am
Sweet…….exactly what i needed!!!!!!!! thx!
November 23rd, 2007 at 1:51 am
Hey Anis!
Thats a very nice piece of code you’ve crafted. I would like to suggest a small tweak, to make life easy for people who wants to implement this.
So, instead of writing these code:
$(function(){
$(‘#comment’).keyup(function(){
limitChars(‘comment’, 20, ‘charlimitinfo’);
})
});
…how about making it more simple like this:
$(function(){
limitChars(‘comment’, 20, ‘charlimitinfo’);
});
…and you hook the things up for the keyup event in your function itself ? In this way, i won’t need to bother whether its keyup or keypress, as you’ll choose whats best.
Thanks
Emran
November 29th, 2007 at 2:04 am
Thanx Emran vai,
It’s simply Excelent!
I cannot understand how did I miss this logic.
Thanx a lot again.
December 5th, 2007 at 10:42 am
Sweeeeeeet and elegant! Thank you so much!
December 15th, 2007 at 5:15 am
very interesting, but I don’t agree with you
Idetrorce
January 4th, 2008 at 9:31 am
Very nice code - I will definitely be using this.
And also, that Paulo guy that was asking for the template as far as I can tell he means the template of this actual site (which is indeed very nice), not the javascript.
January 19th, 2008 at 9:21 pm
interesting code..exactly what i was looking for
March 19th, 2008 at 5:25 am
thats it, guy
March 27th, 2008 at 5:29 am
I don’t want newline/linebreaks to be counted as character. So i’ve changed this:
var text = $(’#'+textid).val();
to this;
var text = $(’#'+textid).val().replace(new RegExp( “\\n”, “g” ),”");
Maybe it’s helpfull for someone else.
June 10th, 2008 at 3:32 am
hello,
i’ve searched in the web for counting characters with jquery and i found your page with Characters Remaining Countdown.
now i have question:
how i can manage that the count relate to both textfields.
so for example:
textfield 1: 5 char
textfield 2: 6 char
totalcount: 11 char
i’ve tried to copy the textfield and name it the same, but did not work.
how can i get this?
August 9th, 2008 at 10:55 pm
Thousands and look closer buy cytotec then announced estivities.
August 12th, 2008 at 10:25 am
@admin:
Hi,
im having this error “too many characters in character literal”
hers’s my code:
Mailing Address
(if different from address of Organisation, max 200 characters.)
:
Write your comment within 200 characters.
August 22nd, 2008 at 5:40 am
Hello, great code, but i was still able to write more than the allowed characters by typing and clicking elsewhere in the website without releasing the key…
I switched the event from keyup to keydown….
Looking like this:
$(’#title’).keydown(function(){
limitChars(’title’, 100, ‘info_title’);
});
And so far it has worked just fine.