Eunice Chia Eunice Chia - 3 months ago 9
jQuery Question

prevent @ to be entered into input field?

I want to prevent

@
to be entered into
input
. But it doesn't work, any idea why?

$(function() {
$(document).on('keyup', '[placeholder="x"]', function() {
if (event.keyCode === 64) {
event.preventDefault();
}
});
})


https://jsfiddle.net/r82wtea3/2/

Answer

You also need to pass parameter event to the function:

$(document).on('keypress', '[placeholder="x"]', function(event) { /*...*/ });

I suggest you to use event.which instead of event.keyCode for better compatibility. Here's your updated fiddle. I have used keypress event instead of keyup.