Prevent User From Writing More Than N Characters In A Textarea Using Prototype Event Observers
Solution 2:
Trim is not a good solution, just try this to know why
Fill the textarea, then go to the middle part and type characters
That makes a horrible effect: The typed characters are taken and then it is trimed, so end text is lost.
maxlength=21 value(with length=21): "This Is What Is Typed" then type before "What" this: "Not " you will get: "This Is Not What Is T"
Desired effect is new typed characters are ignored, rather than discarding the end text.
In the example it is very short... imagine multi-lined textarea... user will continue typing in middle without seeing the end is being discarded.
Try the same example on an input type="text" with maxlength, you will see when limit is reached new typed characters are ignored, no matter where cursor is on the text, at start, midle or end.
Solution 3:
The documentation states that Event#stop
stops propagation as well as prevents default action. Does it not work?
Also, binding it to keydown/keyup doesn't prevent them from pasting large amount of text in the text area.
Like Joel said in his comment, preventing users from typing may not be a good idea. It is much better to put a validation + warning (like in the comment field of Stackoverflow).
Solution 4:
Use setInterval to periodically check state of your textarea, trim text if you want.
Post a Comment for "Prevent User From Writing More Than N Characters In A Textarea Using Prototype Event Observers"