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.
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
To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.
Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.
Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
How to find if a word or a substring is present in the given string. In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false.
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.'); }
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With