$('input').keyup(function() {
var $th = $(this);
$th.val( $th.val().replace(/[^a-zA-Z0-9]/g, function(str) { alert('You typed " ' + str + ' ".
Please use only letters and numbers.'); return ''; } ) );
});
EDIT:
There are some other good answers here that will prevent the input from taking place.
I've updated mine since you also wanted to show an error. The replace can take a function instead of a string. The function runs and returns a replacement value. I've added an alert
to show the error.
http://jsfiddle.net/ntywf/2/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…