Reputation: 919
I'm doing a form validation in jQuery that alerts something when a forbidden character like #$@% is input by user. The problem is, when a user enters those characters, he uses the SHIFT key, so there are two keyup's.
How can I prevent the second one from happening?
if (!regex.test(lastCharacter)
alert('forbidden character');
Thanks a lot
Upvotes: 0
Views: 578
Reputation: 10874
You can bind on the input event which emits when input is changed, however this is only supported since IE9. Before that you can bind on the propertychange event (in IE only that is) which occurs when the value of an input changes. The example below will work in most browser also it might trigger the callback twice in IE9 so might need to check for input event support and act accordingly instead.
My bad here is the one with propertychange in it, try in IE if it doesn't work you might have to assign it the old way.
$('#boo')[0].onpropertychange = function() {};
Upvotes: 1
Reputation: 53301
I have no idea if this will work, but you get the idea:
$("#myInput").keyup(function(e) {
if(e.which == 16) {
$(this).keyup(function(event) {
if(event.which==16) event.preventDefault();
});
setTimeout(function() {
$("#myInput").off('keyup');
},1000);
}
});
Upvotes: 1