Simple JQuery TextArea MaxLength Function
Sunday 29 August 2010 12:57 PM
There are about 25,900 TextArea Maximum Length functions out there, Google said. I searched for a simple function that work on typing (Key Press) and on Copy and Paste, and able to use across the site without calling from each textarea. After few minutes of Googaling, I did not find any that fit my requirements, so here I put together a simple function
You need JQuery to run this. (Download Here). (Here are the JQuery Basics). Copy and Paste bellow code to any where in your web page, and that is all.
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>
Here is a quick Demo (Maximum 50 Characters)
Codes of the above Demo
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>
Posted by Saman W Jayasekara at Tuesday 18 May 2010 03:38 PM .
JavaScript

Stella Auyeung
on 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
on 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
on 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
on 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
on 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: