A Simple JQuery TextArea MaxLength Function

According to Google, there are about 25,900 TextArea Maximum Length functions available. I searched for a simple function that would work with typing (Key Press) and with Copy and Paste. I also wanted to be able to use it across the site without calling from each textarea. After a few minutes of Googling, I didn’t find anything that fit my requirements, so I put together my own simple function instead.

You need JQuery to make this function work. Download it here, and here are the JQuery basics. Copy the code to anywhere on your Web page, and then paste it below. That it’s it.

   1: <script type="text/javascript">
   2: $("document").ready(function() {
   3:  $('textarea[maxlength]').live('keyup change', function() {
   4:  var str = $(this).val()
   5:  var mx = parseInt($(this).attr('maxlength'))
   6:  if (str.length > mx) {
   7:     $(this).val(str.substr(0, mx))
   8:     return false;
   9:  }})})
  10: </script>
Show/Hide Line Numbers . Full Screen . Plain

Here is a quick Demo (Maximum 50 Characters)


And here are the codes of the Demo above

   1: <script type='text/javascript' src='http://cflove.org/js/jquery-1.4.2.min.js'></script>
   2: <script type="text/javascript">
   3: $("document").ready(function() {
   4:  $('textarea[maxlength]').live('keyup change', function() {
   5:  var str = $(this).val()
   6:  var mx = parseInt($(this).attr('maxlength'))
   7:  if (str.length > mx) {
   8:     $(this).val(str.substr(0, mx))
   9:     return false;
  10:  }
  11:  }
  12:  )
  13: })
  14: </script>
  15: <textarea maxlength="50" style="width:400px; height:50px"></textarea>
Show/Hide Line Numbers . Full Screen . Plain

13 Comments :
DAHM
Wednesday 25 March 2015 01:44 AM
thanks you!
Friday 12 April 2013 04:01 AM
GOOOD~~~~~~~~~~~~~~~~~~~~~
Tuesday 05 March 2013 01:03 PM
d
Rafael
Wednesday 06 February 2013 10:37 AM
Hey guys. Textarea maxlength is a html5 property. So its not gonna work in IE :P
Instead of using "var mx = parseInt($(this).attr('maxlength'))" and put ur limit in the maxlength, put ur limit in this mx variable. like var mx = 50;
Hope it help.
Sudharshan
Tuesday 11 December 2012 06:22 AM
Its not working at all in IE :(
Reddy
Tuesday 11 December 2012 06:23 AM
Yes..it NOT WORKING when pasting with mouse!!!
Tuesday 11 December 2012 11:16 AM
In sub standard browsers it triggers at onChange.

Tuesday 11 December 2012 11:16 AM
Tested this with last 3 IE versions. What ver you are on?
Stella Auyeung
Friday 14 October 2011 03:07 AM
Sorry, I just don't seem to be able to paste my html code in this box without being translater. Basically , cut and paste your code and then ran it in IE and it didn't work. The only thing I changed is including jquery v1.6.4 . as "" instead of including your ' jquery-1.4.2.min.js'
Stella Auyeung
Thursday 13 October 2011 12:58 PM
Hi:
I have a question about your above code. I assume I have to place the above code into every web page on my site where there is a textarea. Can I put the following into a js file so on each of my web page I just have to include that js file using a
})


Will it work?
Stella
Thursday 13 October 2011 07:13 PM
Copy follwing bit into a .JS file and include that file on the header of your page(s)

$("document").ready(function() {
$('textarea[maxlength]').live('keyup change', function() {
var str = $(this).val()
var mx = parseInt($(this).attr('maxlength'))
if (str.length > mx) {
$(this).val(str.substr(0, mx))
return false;
}
}
)
})
Stella Auyeung
Sunday 16 October 2011 04:39 PM
Sam:
I sent you an email with my code, not sure if you received it. I have another related question...
Per your direction, I included the following code in a js file and then include that js file in the header of my jsp. Is that all I have to do? i.e., the 'textarea' below in my jsp file will 'automatically' be checked for maxlength?Do I also need to download query.js into my .js directory?


Stella Auyeung
Friday 14 October 2011 02:56 AM
Hi Matt:
I copied your code and placed into a file "Maxlength.html" and then downloaded "jquery.1.6.4.js" (latest from JQuery.com). It does not work. It does not limit to 50 chars. Do you see anything wrong with it?

Here's Maxlength.html: