A bank in Scandinavia recently had a lawsuit because a customer accidentally transferred a pile of money to the wrong account because she typed in too many zeros into the account number. This made the resulting account number invalid, however she didn't notice because the web-application silently erased her extra digit. So she entered something like
1223300045
but should have entered
122330045
and consequently account
122330004
received her money. This is a major usability flaw.
In the case of something like a text-area, take a look at StackOverflow's own comments UI. It shows you how much text is in your textbox, and you have the opportunity to edit your text, but the really sweet part is that you can type as much as you want, then edit down to the bare limit, letting you ensure you can say all you like. If your text box erases the user's text:
- They may not notice it happening, if they type by looking at the keyboard
- They have to erase text before they can add new, more concise wording
- They will be annoyed
Thus, my recommendation is to let the user type as much they want, but let them know when they are over the limit, and let them edit. The following sample can get you started. You should change the selectors as appropriate to only manipulate the textareas/text inputs you need to. And don't forget to do the appropriate thing if the limit is wrong. This sample sets a class; the class attribute lets you change the colour of the textarea (or whatever). You might want to show a message or something. They key is to let the user keep typing.
function checkTALength(event) {
var text = $(this).val();
if(text.length > 140) {
$(this).addClass('overlimit');
} else {
$(this).removeClass('overlimit');
}
}
function checkSubmit(event) {
if($('.overlimit', this).length > 0) { return false; }
}
$(document).ready(function() {
$('textarea').change(checkTALength);
$('textarea').keyup(checkTALength);
$('form').submit(checkSubmit);
});