I don't know how to solve this problem:
The user can start typing some numeric values in the input fields. The cursor will move to the next input field, after typing a number. But it doesn't work, if the user is typing too fast, that means if there is no keyup between two numbers.
So if the user is typing "12" - instead of "1" and "2" -, there should be the value "1" in the first input field, value "2" in the second input field and the focus should be set to the third input field.
<form>
<input type="password" maxlength="1" name="pin1" autofocus />
<input type="password" maxlength="1" name="pin2" />
<input type="password" maxlength="1" name="pin3" />
<input type="password" maxlength="1" name="pin4" />
</form>
$('form input').on('keydown', function(event) {
if (event.shiftKey || event.which <= 47 || event.which >= 58)
return false;
}).on('keyup', function (event) {
if (event.currentTarget.value.length >= 1)
$(event.currentTarget).next('input').focus();
});
http://jsfiddle.net/bbeg17r8/
The KeyDown and KeyUp events are typically used to recognize or distinguish between: Extended character keys, such as function keys. Navigation keys, such as HOME, END, PAGEUP, PAGEDOWN, UP ARROW, DOWN ARROW, RIGHT ARROW, LEFT ARROW, and TAB. Combinations of keys and standard keyboard modifiers (SHIFT, CTRL, or ALT).
If a key is being pressed for a long enough time, it starts to “auto-repeat”: the keydown triggers again and again, and then when it’s released we finally get keyup. So it’s kind of normal to have many keydown and a single keyup. For events triggered by auto-repeat, the event object has event.repeat property set to true.
For example, for reacting to arrow keys Up and Down or hotkeys ( key combinations are also counted). If you want to understand the keyboard events better, you can use the following test stand: The keydown and keyup events occur whenever the user presses a key. The keydown happens at the moment the user presses the key down.
The KeyDown event occurs when the user presses a key on a running form while that form or a control on it has the focus. The KeyDown and KeyPress events alternate repeatedly until the user releases the key, at which time the KeyUp event occurs. The form or control with the focus receives all keystrokes.
Have you tried with input
event combining both conditions and it will work for multiple user actions like key events
, cut/paste events
etc.:
$('form input').on('input keypress', function(event) {
if (event.type == "keypress" && (event.shiftKey || event.which <= 47 || event.which >= 58))
return false;
if (event.currentTarget.value.length >= 1)
$(event.currentTarget).next('input').focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="password" maxlength="1" name="pin1" autofocus />
<input type="password" maxlength="1" name="pin2" />
<input type="password" maxlength="1" name="pin3" />
<input type="password" maxlength="1" name="pin4" />
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With