Is there any way to tell from a given keydown
event whether the owner input[type=text]
or textarea
will decrease or increase in length after it's applied?
Background: have an HTML5 site (<!DOCTYPE html>
) that has to support IE 8 and 9 users as the primary consumer. On some pages, I have <textarea>
elements with maxlength="100"
attributes. IE does not recognize the maxlength
attribute, and allows more than 100 characters to be entered into the textarea
.
I have not seen a shim that limits the character length of a textarea
in IE with the same keyboard behavior as a input[type=text]
element.
For that reason, I'm trying to write one.
Existing implementations look something like this:
$('textarea[maxlength]').on('keydown keyup blur', function(e) {
var $this = $(this),
maxlen = $this.attr('maxlength'),
val = $this.val();
if (val.length > maxlen) {
$this.val(val.substr(0, maxlen));
}
});
The problem with above implementation is that you can type or paste 11 characters and the value is not trimmed to the maxlength until after the value has changed. I would like to prevent a change before the value changes, to better align with input[type=text,maxlength]
behavior.