I have a text field, which will accept only the following characters:
Allowed characters: [a-z 0-9 + # - .]
This is the same filter SO>
This worked for me:
$(function(){ $('#t').keypress(function(e){ var txt = String.fromCharCode(e.which); console.log(txt + ' : ' + e.which); if(!txt.match(/[A-Za-z0-9+#.]/)) { return false; } }); });