Limiting Characters in a Textarea with jQuery
Penned on .
Ran into the problem of limiting the characters of a text area on a form recently. In my 8 minutes on google, all of the pure JS solutions felt cumbersome to me so i whipped together this little number in jQuery
$("#textAreaID").keypress(function(e) {
if(e.charCode >= 48 )
if($(this).val().length > 150)
return false;
});
Quick and dirty. JQuery binds a function to the keypress even in the textarea. This function checks if the keyCode of the pressed key is a character input key (see here for a list of keycodes) and, if it is and the character count is over the pre-determined limit, returns false. Easy, right?
Please note, this does require pretty much any version of jQuery to actually work