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

12 comments:

  1. 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.))

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

    ReplyDelete
  3. 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.

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

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

    ReplyDelete
  6. $('.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.

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

    ReplyDelete
  8. Enjoyed the read! Very helpful and informative!

    http://www.marketingusingvideo.com

    ReplyDelete
  9. 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() {...});

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


    Swati

    ReplyDelete