Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to validate a string that is used to create a folder.
so I want to restrict user from entering characters like \ / : ? " < > |
this characters will down the folder creation.
Can anyone help me by giving me the regular expression to validate this characters?

Thank You
[edit]removed white spaces[/edit]
Posted
Updated 7-Apr-11 19:15pm
v2

C#
string abc = "anvas the realnapster /";
char[] SpecialChars = "\/:?<>|".ToCharArray();
int indexOf = abc.IndexOfAny(SpecialChars);
if (indexOf != -1)
{
    //there should a special char in the string
}
else
{
    //string is fine
}
 
Share this answer
 
v2
Comments
Albin Abel 8-Apr-11 1:09am    
Simpler than reg Ex. My 5
Sandeep Mewara 8-Apr-11 1:19am    
My 5! Good solution.
Anvas has the right idea, but .NET provides two methods which return the illegal characters for a file and a path, which you should use instead of defining your own:
GetInvalidPathChars [^]
GetInvalidFileNameChars[^]

if (myFileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
   {
   // Illegal character in file name
   }

Using these provides better portability than specifying your own.
 
Share this answer
 

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