Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

How to check the input string contain only special characters ,using regular expression.

C#
public static bool IsSpecialCharacters(this string stringToTest)
       {
           const string charSet = "[^a-z0-9]";

           //Regex RgxUrl = new Regex("[^a-z0-9]");
           //blnContainsSpecialCharacters = RgxUrl.IsMatch(stringToCheck);

           return Regex.Match(stringToTest, @"^[" + charSet + @"]+$").Success;
       }


Is this method is correct?
Posted
Updated 18-May-23 3:20am
Comments
Zoltán Zörgő 16-Apr-13 3:56am    
do not double square brackets
Prasad Khandekar 16-Apr-13 4:00am    
Hello,

The regular expression should work. However all characters other than a-z,0-9 will mbe treated as special characters even uppercase letters and whitespace. If you don;t want that to happen then change it to [^a-zA-Z0-9\s]+. You can use Expresso (http://www.ultrapico.com/) regular expresion tester to test your expressions.

Regards,
Maciej Los 16-Apr-13 4:02am    
Please, post it as an answer.
My virtual 5!
paracha1 9-Nov-17 10:19am    
you can use only this simple way. its working fine

public static bool HasSpecialChars(string stString)
{
if (stString.Any(ch => !Char.IsLetterOrDigit(ch)))
{
return true;
}
else {
return false;
}
}

you can simply check like below.

just add namespace

using system.linq

int PasswordSpecialChar = txtMonth.Text.Count(p => !char.IsLetterOrDigit(p));
 
Share this answer
 
v2
No.
Your solution will not work with upper case, for example. Try this:
C#
return Regex.Match(stringToTest, @"^\W+$").Success;
Which matches everything that is not alphanumeric.
 
Share this answer
 
Comments
Arjun Menon U.K 13-Nov-13 6:37am    
Hi,
\w --> checks for [A-Za-z0-9_] and
\W--> checks for characters other than [A-Za-z0-9_]
rt?
So what if i have to check only alphanumerics a-z or -0-9 or combination of both.... Currently am using this
Regex regexUSerName = new Regex("^[a-zA-Z0-9 ]*$");
OriginalGriff 13-Nov-13 7:19am    
Don't hijack threads that aren't directly related to your question - ask a new question instead. Otherwise it is rude - particularly with an old question!
Arjun Menon U.K 20-Nov-13 5:58am    
My apologies...
public static bool hasSpecialChar(string input)
{
string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,*+";
foreach (var item in specialChar)
{
if (input.Contains(item.ToString())) return true;
}

return false;
}
 
Share this answer
 
Comments
Richard Deeming 18-May-23 9:34am    
Aside from the fact that you're more than ten years late on this question, that's a horrible solution!

At the very least, you should remove the .ToString() call within the loop. The Contains method has had an overload which accepts a char since .NET Core 2.1; if you're still stuck in .NET Framework, you can use IndexOf instead:
if (input.IndexOf(item) != -1) return true;

A better option would be to use a char array and call IndexOfAny:
private static readonly char[] SpecialChars = 
{
    '\\', '|', '!', '#', '$', '%', '&', '/', '(', ')', 
    '=', '?', '»', '«', '@', '£', '§', '€', '{', '}', 
    '.', '-', ';', '\'', '<', '>', '_', ',', '*', '+'
};

public static bool HasSpecialChar(string input) => input != null && input.IndexOfAny(SpecialChars) != -1;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900