It's annoyed me for quite a while that you can set a maximum length on an <input type="text" /> but not on a textarea. Why? Are they so different?
I immediately thought I was going to have to write some messy JavaScript but then I learned that HTML5 implements maxlength on text areas now and I'm only considering modern browsers! Wahoo!
Then I learnt that IE9 doesn't support it so JavaScript it is...
JavaScript textarea maxlength limiter
What I have done that works well is bind events keyup and blur on my text area to a function that removes characters over the maxlength provided. The code looks like this:
$('#txtMessage').bind('keyup blur', function () {
var $this = $(this);
var len = $this.val().length;
var maxlength = $this.attr('maxlength')
if (maxlength && len > maxlength) {
$this.val($this.val().slice(0, maxlength));
}
});
Conclusion
It works quite well because if the browser already supports maxlength on a textarea there will be no interruption because the value of the textarea will not go over that maxlength.
The keyup event doesn't fire when the user pastes text in using the mouse but that is where the blur event comes in. Also, an enter click makes a new line on a textarea so the user has to click the submit button (blurring the textarea).
Beware though that this is for usability only; a nasty user could easily bypass this so ensure you are checking the length server side if it is important to you.

