Please see my comment to the question. Deleting wrong character is not a valid option. Instead, you should filter out some unwanted characters, so they won't even appear in the input.
For this purpose, your event handler should have an additional argument (which you need anyway), the instance of the event object, let's say, its name is
event
. On certain condition, you want to block some character from input. This is done by calling the function
event.preventDefault()
:
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault[
^].
(See also:
Event.defaultPrevented — Web APIs | MDN[
^],
Event.stopPropagation() — Web APIs | MDN[
^].)
It does not matter if you do it using jQuery or not. Please see this jQuery help page:
.keypress() | jQuery API Documentation[
^].
On this page, please find the sample code fragment with
event.preventDefault();
from this code sample, you will see how to use this technique with jQuery.
—SA