9
I am creating a system in which the user must type in a input the mother’s name with only alphanumeric characters. I am using the code below that is working, but I noticed a flaw. Even allowing not to enter other special characters, if the user copy and paste another special character it will be inserted into the input.
<input type="text" id="nomeMae">
$('#nomeMae').bind('keypress', function (event)
{
var regex = new RegExp("^[0-9a-zA-Z\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key))
{
event.preventDefault();
return false;
}
});
More pleasant in terms of usability is to let paste freely and remove after only what does not serve the field. And take advantage to do what everyone forgets after removing characters: leave the cursor in the correct place of the new string.
– Bacco