Check for special characters are not allowed in C#

34,979

Solution 1

^[\%\/\\\&\?\,\'\;\:\!\-]+$

matches the strings that consist entirely of special characters. You need to invert the character class to match the strings that do not contain a special character:

^[^\%\/\\\&\?\,\'\;\:\!\-]+$
  ^--- added

Alternatively, you can use this regex to match any string containing only alphanumeric characters, hyphens, underscores and apostrophes.

^[a-zA-Z0-9\-'_]$

The regex you mention in the comments

[^a-zA-Z0-9-'_]

matches a string that contains any character except those that are allowed (you might need to escape the hyphen, though). This works as well, assuming you reverse the condition correctly (accept the strings that do not match).

Solution 2

If you are just looking for any of a list of characters then a regular expression is the more complicated option. String.IndexOfAny will return the first index of any of an array of characters or -1. So the check:

if (input.IndexOfAny(theCharacetrers) != -1) {
  // Found one of them.
}

where theCharacetrers has previously been set up at class scope:

private readonly char[] theCharacetrers = new [] {'&','\','/','!','%','#','^',... };

Solution 3

You needed to remove ^ from the beginning and $ from the end of the pattern, otherwise in order to match the string should start and end with the special characters.

So, instead of

@"^[\%\/\\\&\?\,\'\;\:\!\-]+$"

it should be

@"[\%\/\\\&\?\,\'\;\:\!\-]+"

You can read more about start of string and end of string anchors here

Solution 4

Your RegExp is "string consiting only of special characters (since you have begin/end markers ^ and $).

You probably want just check if string does not contain any of the characters @"[\%\/\\\&\?\,\'\;\:\!\-]") would be enough.

Also String.IndexOfAny may be better fit if you just need to see if any of the characters is present in the source string.

Share:
34,979
ankur
Author by

ankur

Updated on July 05, 2022

Comments

  • ankur
    ankur almost 2 years

    I have to validate a text box from a list of special characters that are not allowed. This all are not allowed characters.

    "&";"\";"/";"!";"%";"#";"^";"(";")";"?";"|";"~";"+";" ";
                                                       "{";"}";"*";",";"[";"]";"$";";";":";"=";"
    

    Where semi-column is used to just separate between characters .I tried to write a regex for some characters to validate if it had worked i would extend it.it is not working . What I am doing wrong in this.

    Regex.IsMatch(textBox1.Text, @"^[\%\/\\\&\?\,\'\;\:\!\-]+$")
    
  • ankur
    ankur over 11 years
    Thanks @Jan your change worked ,After going through my list of characters that are not allowed. I am only allowing this set [A-z,a-z,0-9] any thing out side this should not be allowed. So if I only add the small check for this [A-z,a-z,0-9] would be easy rather than writing the complete list. So is this correct "[^a-zA-Z0-9-'_]" for the small set.
  • JoshYates1980
    JoshYates1980 about 7 years
    What is the datanotation for not accepting a string value of "-1" for a viewmodel?