Javascript jQuery:检查字符串中是否存在特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13840143/
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 14:53:56  来源:igfitidea点击:

jQuery: Check if special characters exists in string

javascriptjqueryspecial-characters

提问by Guido Visser

I know this question is asked more often here on Stack, but I can't seem to get a straight answer out of the questions already posted.

我知道这个问题在 Stack 上被问得更频繁,但我似乎无法从已经发布的问题中得到直接的答案。

I need to check if all special characters (except -) are in a string, if so, then give the user an alert.

我需要检查所有特殊字符(-除外)是否都在字符串中,如果是,则向用户发出警报。

What I have so far is this:

到目前为止,我所拥有的是:

if($('#Search').val().indexOf('@') == -1 || $('#Search').val().indexOf('#') == -1 || $('#Search').val().indexOf('$') == -1 || $('#Search').val().indexOf('%') == -1 || $('#Search').val().indexOf('^') == -1 || $('#Search').val().indexOf('&') == -1 || $('#Search').val().indexOf('*') == -1 || $('#Search').val().indexOf('(') == -1 || $('#Search').val().indexOf(')') == -1 || $('#Search').val().indexOf('_') == -1 || $('#Search').val().indexOf('\'') == -1 || $('#Search').val().indexOf('\"') == -1 || $('#Search').val().indexOf('\') == -1 || $('#Search').val().indexOf('|') == -1 || $('#Search').val().indexOf('?') == -1 || $('#Search').val().indexOf('/') == -1 || $('#Search').val().indexOf(':') == -1 || $('#Search').val().indexOf(';') == -1 || $('#Search').val().indexOf('!') == -1 || $('#Search').val().indexOf('~') == -1 || $('#Search').val().indexOf('`') == -1 || $('#Search').val().indexOf(',') == -1 || $('#Search').val().indexOf('.') == -1 || $('#Search').val().indexOf('<') == -1 || $('#Search').val().indexOf('>') == -1 || $('#Search').val().indexOf('{') == -1 || $('#Search').val().indexOf('}') == -1 || $('#Search').val().indexOf('[') == -1 || $('#Search').val().indexOf(']') == -1 || $('#Search').val().indexOf('+') == -1 || $('#Search').val().indexOf('=') == -1)
{
   // Code that needs to execute when none of the above is in the string
}
else
{
  alert('Your search string contains illegal characters.');
}

But this doesn't seem to work... Can anyone help me on this matter?

但这似乎不起作用......有人可以帮我解决这个问题吗?

Thanks in advance!

提前致谢!

Guido

圭多

回答by bfavaretto

If you really want to check for all those special characters, it's easier to use a regular expression:

如果您真的想检查所有这些特殊字符,使用正则表达式会更容易:

var str = $('#Search').val();
if(/^[a-zA-Z0-9- ]*$/.test(str) == false) {
    alert('Your search string contains illegal characters.');
}

The above will only allow strings consisting entirely of characters on the ranges a-z, A-Z, 0-9, plus the hyphen an space characters. A string containing any other character will cause the alert.

上面只允许字符串完全由范围内的字符组成a-zA-Z, 0-9, ,加上连字符和空格字符。包含任何其他字符的字符串将导致alert.

回答by Anton

var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\,./~`-="
var check = function(string){
    for(i = 0; i < specialChars.length;i++){
        if(string.indexOf(specialChars[i]) > -1){
            return true
        }
    }
    return false;
}

if(check($('#Search').val()) == false){
    // Code that needs to execute when none of the above is in the string
}else{
    alert('Your search string contains illegal characters.');
}

回答by Jay Blanchard

You could also use the whitelist method -

您也可以使用白名单方法 -

var str = $('#Search').val();
var regex = /[^\w\s]/gi;

if(regex.test(str) == true) {
    alert('Your search string contains illegal characters.');
}

The regex in this example is digits, word characters, underscores (\w) and whitespace (\s). The caret (^) indicates that we are to look for everything that is not in our regex, so look for things that are not word characters, underscores, digits and whitespace.

本例中的正则表达式是数字、单词字符、下划线 (\w) 和空格 (\s)。插入符号 (^) 表示我们要查找不在正则表达式中的所有内容,因此请查找不是单词字符、下划线、数字和空格的内容。

回答by JJJ

You are checking whether the string contains allillegal characters. Change the ||s to &&s.

您正在检查字符串是否包含所有非法字符。将||s更改为&&s。