Rancid said:
I've tried searching the web but I've had little success finding a
cross-browser(IE, Firefox and Opera anyway) implementation of a
restricted text INPUT that works in realtime before a character is even
displayed in the field. Is there a working implementation anywhere?
The HTML maxlength attribute is pretty robust. There are many scripts
availble, search the archives of this news group. They are of limited
use as they only work if scripting is enabled - which probably creates
more issues than cross-browser compatability.
If you want to stop something from being displayed, you have to use the
keydown event to see which key(s) have been pressed and work out
whether to cancel the event or allow it to proceed. That logic can
become extensive, as once you have reached your maximum number of
characters you still want to allow key strokes like delete, backspace,
cursor keys, etc.
Users can also select multiple characters and enter a single character
to replace them, hence the use of keyup or keypress events to see what
the user actually did before trying to modify their behaviour.
Therefore most scripts wait until the user has finished entering text
before trying to validate it.
A good solution is to use an on-screen message based on keypress to
advise of invalid input rather than trying to modify the input's value.
The message appears when invalid input is detected, then disappears
when it is fixed. It keeps the user in control and doesn't try to
force them into any particular behaviour - how they achieve valid input
is totally up to them. The value of an input only has to be valid
*when the form is submitted*, not at some random point in time before
then.
And how about denial of cross-browser pasting? On Windows a popup menu
appears with the clipboard pasting options and I'd like to disable that
and CTRL-V in an INPUT too.
Impossible to do reliably in even a few browsers, much less all.