I am using the following code for my validation that only allows letters and numbers and a few allowed characters.
$('input').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9%()#@-_& ]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
The only problem I am having is that if you copy and paste any un allowed characters then this doesn't prevent it. I know I have seen this done somewhere where it prevents the characters being pasted. Any thoughts?