How to prevent non-alphanumeric input in javascript?

前端 未结 5 1567
暖寄归人
暖寄归人 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:05

    THis line

    regx.test('#user')
    

    has you testing the string #user, and that is a string that has a bad character (the #). So it will always say not allowed.

    Use the actual value of your $("#user") there by using $(this).val()

提交回复
热议问题