$(\"#user\").keyup(function(e){
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test(\'#user\'))
{$(\"#infoUser\").html(\"Alphanumeric only allowed !\");}
);}
<
change:
if (!regx.test('#user'))
to
if (!regx.test( $(this).val() ) )
Do:
$("#user").keyup(function(e){
var str = $.trim( $(this).val() );
if( str != "" ) {
var regx = /^[A-Za-z0-9]+$/;
if (!regx.test(str)) {
$("#infoUser").html("Alphanumeric only allowed !");
}
}
else {
//empty value -- do something here
}
});
JS Fiddle example