简体   繁体   English

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

[英]jQuery: Check if special characters exists in string

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 圭多

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 az , AZ , 0-9 , plus the hyphen an space characters. 以上内容仅允许字符串由azAZ0-9范围内的字符组成,加上连字符和空格字符。 A string containing any other character will cause the alert . 包含任何其他字符的字符串将导致alert

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.');
}

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). 此示例中的正则表达式是数字,单词字符,下划线(\\ w)和空格(\\ 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. 插入符号(^)表示我们要查找不在我们的正则表达式中的所有内容,因此请查找不是单词字符,下划线,数字和空格的内容。

You are checking whether the string contains all illegal characters. 您正在检查字符串是否包含所有非法字符。 Change the || 改变|| s to && s. s到&& s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM