user1683987
user1683987

Reputation: 533

How do I not allow special characters, but allow space in regex?

I need to populate an error message when the user is entering special characters within a datagridview. as of now I have the following:

if (Regex.IsMatch(columnValue.ToString(),"^[A-Za-z0-9]$"))
                {
                    MessageBox.Show("You may not use special characters.", "Saving not allowed at this time", MessageBoxButtons.OK, MessageBoxIcon.Error);  

this regex works, and does not allow special character, however it is falls in the if statement if there is a space between characters, which should be allowed.

How do I make it to allow a space, but not special characters?

Thanks,

Upvotes: 4

Views: 51377

Answers (5)

Jason Whitted
Jason Whitted

Reputation: 4024

I think you just want to add a space to your regular expression:

if (Regex.IsMatch(columnValue.ToString(),"^[ A-Za-z0-9]$"))

When writing regular expressions it is generally a good habit to use the verbatim string operator @ as many of the special expressions require you to use a backslash character.

if (Regex.IsMatch(columnValue.ToString(), @"^[ A-Za-z0-9]$"))

If you were explicitly telling the regular expression to ignore whitespace in the pattern you would then need to escape the space character.

if (Regex.IsMatch(columnValue.ToString(), @"^[\ A-Za-z0-9]$", RegexOption.IgnorePatternWhitespace))

If you wanted them to be able to use any whitespace characters (spaces, tabs, newlines, etc.), you could use the whitespace character class \s

if (Regex.IsMatch(columnValue.ToString(), @"^[\sA-Za-z0-9]$"))

Update

It appears that you may have wanted to a regular expression that excluded characters in a group.

Your current regular expression is looking from the beginning of the string ^ to the end of the string $ for exactly one character in the group A-Za-z0-9. If you want to match the exact opposite you could use the not operator ! before your Regex.IsMatch, like:

if (!Regex.IsMatch(columnValue.ToString(), @"^[ A-Za-z0-9]$"))

But if you wanted to write an excluded group of characters in your regular expression you can put a ^ in your group. When the ^ is inside of a group [^] any characters that come after it are excluded from the match.

The regular expression [A-Z] would match any character A-Z. The regular expression [^A-Z] would match any character NOT A-Z.

If that is what you were looking for you could change your statement to:

if (Regex.IsMatch(columnValue.ToString(), @"[^A-Za-z0-9\ ]"))

This statement will match any string that contains any character not in the group. So if they type "Hello World" it would not match. If they typed "Hello! World" it would match because "!" is not in the group.

NOTE: The leading ^ and trailing $ were removed. In this regular expression we don't care about the length of the string as we are only matching on a character we were not expecting anywhere in the string.

Upvotes: 13

MethodMan
MethodMan

Reputation: 18863

if (Regex.IsMatch(columnValue.ToString(),"^[A-Za-z0-9 ]+$"))
{
   MessageBox.Show("You may not use special characters.", "Saving not allowed at this time", MessageBoxButtons.OK, MessageBoxIcon.Error); 
}

or as a single Boolean check to test

bool isValid = Regex.IsMatch(columnValue, "^[a-z0-9 ]+$", RegexOptions.IgnoreCase);

Upvotes: 3

Aniket Inge
Aniket Inge

Reputation: 25723

Why not try this regex: [a-zA-Z0-9\s]+

Upvotes: 0

bonCodigo
bonCodigo

Reputation: 14361

You could include all the digits, strings and space :)

"^[a-z]|[A-Z]|[0-9]|[ ]$"

or

"^[\d+]|[\w+]|[ ]$"

Upvotes: 1

SWeko
SWeko

Reputation: 30912

Add a space in the character class, i.e. use ^[A-Za-z0-9 ]$ instead of ^[A-Za-z0-9]$. This will still disallow other whitespace characters, like tab.

Upvotes: 0

Related Questions