If your goal is to prevent not just being typed , but that it be used as a value anyway (eg copy and paste) I suggest listening to the input
property and adjusting the value according to your rule (in this case, delete the commas):
$('input').on("input", function(e) {
$(this).val($(this).val().replace(/,/g, ""));
});
Example in jsFiddle. In some older browsers, you may also need to hear by propertychange
(but I believe which input
is widely supported).
That way, it does not matter if the comma was typed on the "normal" keyboard, on the numeric keypad, Ctrl + C Ctrl + V or even Right-click and "paste". The value will be kept without the comma in all cases, and no visual glitch will occur.
P.S. See also this question I did some time ago in SOEN, for more details.