Regular expression allowing special character for first key press

Regular expression allowing special character for first key press

Hi,
 
i am trying to use regular expression in jquery on keypress function to allow only Alphanumeric values from text field, but if i press any special character then it is allowing for the first time
 
jquery code
$(
function () {

$(

"#legalDiv" ).find( ".legalClass" ).bind( "keypress" , function (){

if ( this .value.match(/[^0-9a-zA-Z]/g)) {

return false ;

}

});

});
 
Html code
<
div id="legalDiv"
> Legal Name: < input type = "text" name = "legalName" class = "legalClass" id = "legalId" ></ input ></ div >
 
output getting on key press(it is allowing first specail character pressed )
wert@dfrt234 or wsd!dfr
 
Thanks in advance for any help