I'm trying to write a regex for an name field and block all special characters
JS Fiddle: https://jsfiddle.net/69mqhzq6/
However, my code seems to ignore it. Could someone tell what I'm doing wrong?
$('input').on('keypress', function (e) {
var blockSpecialRegex = new RegExp("~`!@#$%^&()_={}\[\]\:;,.\/<>/-+/?");
var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
console.log(key)
if(blockSpecialRegex.test(key) || $.isNumeric(key)){
e.preventDefault();
return false;
}
});