Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 795
Guido Visser Avatar asked Dec 12 '12 12:12

Guido Visser


People also ask

How do I check if a string contains special characters?

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.

How do you check if a string contains a specific character in JavaScript?

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.

How do you check special characters in regex?

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 do I search for a specific word in a string using jQuery?

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.


2 Answers

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.'); } 
like image 34
Anton Avatar answered Sep 17 '22 18:09

Anton


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.

like image 73
bfavaretto Avatar answered Sep 20 '22 18:09

bfavaretto