Reputation: 848
I wanted to make an input field to react to invalid inputs dynamically in such a way that a popup (with window.alert) will be displayed to inform the players of the invalid input. However, after I close the popup, the very same popup will be displayed twice with very short time interval between. The input can be changed after this though.
How can I solve it? Would there be a way to make sure that the popup is not displayed again for a couple of second?
Relevant code:
$(':input').bind('keypress keydown keyup change',function(){
var weight = parseFloat(String($(':input[name='.concat(n,']')).val()).replace(',', '.'));
if (weight > 100 || weight < 0){
window.alert("Bitte eine Nummer zwischen 0 und 100 eingeben!");
}
});
Upvotes: 0
Views: 391
Reputation: 49582
You are binding 4 events to do the same thing, so you should get 4 alerts:
All 4 event fires because you press the key, and release the key, and since the element looses focus when you do the alert, also the change-event is fired.
You forgot the input-event to be complete...
Use the change-event, to only run your code when the value is committed by the user.
Upvotes: 1