See this example of using only a specific character set (whitelist), which IMO is better and safer:
var allowed = /[a-ZA-Z0-9]/;
window.onload = function () {
var input = document.getElementById("test");
input.onkeypress = function () {
var evt = arguments[0] || event;
var char = String.fromCharCode(evt.which || evt.keyCode);
if (!allowed.test(char)) {
evt.cancelBubble = true;
return false;
}
}
};
C:
prevent input of characters without ascii in the text field
Alternatively, you can use regex to highlight characters without ascii.
see here: How to remove all non-ASCII characters from a string in Ruby