Limit Number of Characters in a TextArea using jQuery

The MaxLength property works for a textbox but not for a TextArea. I recently bumped into a JavaScript solution which demoed how to Limit the Number of Characters in a Textarea or Text Field. I thought of replicating the functionality in jQuery and here’s what I came up with

<!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>
<
title>Limit Number of Characters in a TextArea</title>
<
script type='text/javascript'
src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>
<
script type='text/javascript'>
$(document).ready(function() {
$('#ta').keyup(function() {
var len = this.value.length;
if (len >= 150) {
this.value = this.value.substring(0, 150);
}
$('#charLeft').text(150 - len);
});
});
</script>
</
head>
<
body>
<
textarea id="ta" cols="20" rows="8"></textarea><br/>
(Maximum characters: 150)<br/>
<
span id="charLeft"> </span> Characters left
</body>
</
html>



OUTPUT

image

Note: Although I have shown you how to disallow additional characters, this can be confusing to novice users. So instead of disallowing additional characters, let your users know that they crossed the TextBox limit and then allow them to edit it before submitting.

Check a Live Demo here






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

12 comments:

Anonymous said...

It has some bugs :
1.) On Firefox, it takes me to the top of the textarea if the limit is exceeded.
2.) On Firefox, it allows to me to exceed the limit on certain occasions (I was able to get it to display negative number of characters left (-1, -2, etc.))

Unknown said...

this doesn't stop copying/pasting more than the max characters either.

Suprotim Agarwal said...

Anonymous: In FireFox, although the negative number is displayed for a moment while typing quickly, however it does not accept more than 150 characters. The extra string is trucated

Steve: I have tested this in IE7, 8, Firefox 3 and Chrome and it truncates the extra characters, even when it is copied and pasted.

Coldfusion developer said...

Sweet as!!! Thanks.

Webcliq said...

Yours was the solution that worked ....
I wanted to use jQuery solution and everything else including Plugins failed miserably.

Giedrius said...

if you use >=, you can't select all text, if input text length = limit.

Anonymous said...

$('.desc').keyup(function() {
var len = this.value.length;
if (len >= 200) {
this.value = this.value.substring(0, 200);
}
if (len >= 200) {
$('.charleft').text('0');
}
else {
$('.charleft').text(200 - len);
}
});

Makes it less buggy in firefox.

Anonymous said...

The bug in this script is not acceptable. Sorry, but this was not useful.

Suprotim Agarwal said...

Which Bug?

Anonymous said...

Enjoyed the read! Very helpful and informative!

http://www.marketingusingvideo.com

Anonymous said...

onkeyup doesn't intercept paste from the context menu. you should add the paste and/or input events, i.e. use .bind("keyup paste input", function() {...});

Swati said...

Thanks for Limit the no. of charachters in textarea coding. But
waht about control charachters and same I want in Javvascript


Swati