username html field
Username: <input type="text" name="username" id="username" />
keyup() event
$(document).ready(function(){
$('#username').on('keyup', function(){
var username = $("#username").val();
alert(username);
});
});
change() event
$(document).ready(function(){
$("#username").change(function() {
var username = $("#username").val();
alert(username);
});
});
My question is should I use keyup()
event or change()
event to do username verification?
Occur in sequence when a user presses and releases a key. KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key.
The change event occurs if the value has been changed when the element loses focus. The keypress event occurs every time you press down and release a (non control) key.
keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key. keyup – fires when any key is released, fires last, and the browser processes the key.
keypress Fires when an actual character is being inserted in, for instance, a text input. It repeats while the user keeps the key depressed. keyup Fires when the user releases a key, after the default action of that key has been performed.
They are almost the same thing. In jQuery or JavaScript, I would have to recommend the change()
event. The reason you should not use keyup()
is because if a user inputs a value using autofill, it will not fire the keyup()
event. However, autofill does fire the change()
event, and your verification script will run, and the input will be verified.
NOTE: Normally, change()
is fired when the element loses focus. If you want to check data after every key press, you could combine keyup()
and change()
, which would allow you to parse the data on both events. This is the best of both worlds in my opinion.
Hope this helps!
Use keyup with debouncing, it's more user friendly.
Whenever you release a key.
The keyup event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type. -- http://api.jquery.com/change/
Whenever the content of that field changes, generally, it happens when you remove focus from that field, but not only that.
The change event is sent to an element when its value changes. This event is limited to input
elements, textarea
boxes and select
elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus. -- http://api.jquery.com/change/
Use keyup and a debounced callback So that you verification process isn't fired after each key stroke, but only if the user stops typing, check this example: http://codepen.io/desandro/full/JDugF, open the page, open the javascript console, and start scrolling.
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