How to prevent non-alphanumeric input in javascript?

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

       function isNotAlphanumeric(){
            return (! val.match(/^[a-zA-Z]+$/))
            //return val.match(/^[a-zA-Z0-9]+$/) ? false : true;
       } 
    

    so if val is alpha numeric it return false. So based on returned value take appropriate action. You can call this method on key up event.

提交回复
热议问题