Javascript 如何防止javascript中的非字母数字输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14336195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:25:53  来源:igfitidea点击:

How to prevent non-alphanumeric input in javascript?

javascriptjqueryregex

提问by Alegro

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

#useris a text input, and I want to diplay a warning if user enters anything except letters and numbers.
In the above case, the warning is present whatever is typed.

#user是文本输入,如果用户输入字母和数字以外的任何内容,我想显示警告。
在上述情况下,无论键入什么都会出现警告。

回答by Sudhir Bastakoti

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 Fiddleexample

JS小提琴示例

回答by Nanne

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.

您是否测试了 string #user,这是一个包含错误字符 (the #)的字符串。所以它总是说不允许。

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

使用你$("#user")那里的实际价值$(this).val()

回答by HosseiN

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

您必须使用 #user 元素值而不是 '#user' 字符串进行测试

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

回答by Deenathaiyalan Shanmugam

$('#alpha').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

$('#numeric').bind('keypress', function (event) {
var regex = new RegExp("^[0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

$('#alphanumeric').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});


$('#alphanumericspecial').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9 .]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

回答by rai.skumar

   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.

因此,如果 val 是字母数字,则返回 false。所以根据返回值采取适当的行动。您可以在 key up 事件上调用此方法。