Jquery keypress, keydown, keyup, input working.
I have a requirement such that in a input text box, on each value entered a validation has to occur and after the 6th character is entered a hyphen has to get appended to the string.
The solutions which I tried and the problems I encountered are listed below:
keyup - It is working perfectly for detecting each character entered and appending a hyphen except that "it will send only a single key entry for a long key press". Ex: If I keep '9' pressing for a long time, the value in the text box will be '999999999999' but the keyup event is triggered only once.
keypress - It is working perfectly for detecting each character entered and appending a hyphen except that "the keypress event is not called on a backspace". Ex: If I enter backspace I have a functionality to validate the field again and deleting the backspace.
keydown - I am not getting the exact current value on each key down. Ex: If I enter '9' in the field, in the keydown event, I am getting only '' as the current value. And I type '99', I get '9' as the field value. Because of the fact that on keydown event, the value will not get updated in the field.
input - Everything is working fine, except that I am not able to get the keycode inside this function. (Without which I cannot detect the backspace).
So, what should be done in order to
- work with long key presses,
- Backspace detection,
- current value updated when the key is pressed,
work at the same time.
For the past few hours I keep messing by exploring a lot of things. Probably it is a simple thing, but I am not able to get the answer. Your help would be appreciated a lot. Thanks in advance.