How to prevent non-alphanumeric input in javascript?

前端 未结 5 1599
暖寄归人
暖寄归人 2021-01-04 22:24
$(\"#user\").keyup(function(e){ 
    var regx = /^[A-Za-z0-9]+$/;
    if (!regx.test(\'#user\')) 
    {$(\"#infoUser\").html(\"Alphanumeric only allowed !\");}
);}
<         


        
5条回答
  •  暖寄归人
    2021-01-04 23:18

    You must test with #user element value not '#user' string

    $("#user").keyup(function(e){ 
        var regx = /^[A-Za-z0-9]+$/;
        if (!regx.test($('#user').val()))  // .
        {$("#infoUser").html("Alphanumeric only allowed !");}
    );}
    

提交回复
热议问题