Thursday, 12 September 2013

Javascript denying user input into a field on keydown

Javascript denying user input into a field on keydown

I have a field with a set number of digits to be allowed. This field has a
mask to add commas in the appropriate places. However if the max number if
digits is allowed and the user highlights a comma, they can replace the
comma with a digit - then the mask is applied and the field ends up adding
extra digits. I have a function that catches this on keydown and does an
e.preventDefault(); when the value length is >= allowedDigits for that
field.
The problem is that when the user keys down the number gets placed into
the field then it checks it. Is there any way to remove what the user
pressed or check before the input gets placed into the field?

No comments:

Post a Comment