If Regex is not a requirement, then you could write a stringextension that returns a bool indicating if the value is valid or not.
public static class StringExtensions
{
private static char[] invalidChars = { '<', '>', '&', '%', ';', '=', '{', '}', '(', ')' };
public static bool IsValid(this string value)
{
if (value == null)
{
return false;
}
foreach (char c in invalidChars)
{
if (value.Contains(c))
{
return false;
}
}
return true;
}
}
Then you can check it like this:
static void Main(string[] args)
{
string validString = "Hello World";
string invalidString = "Hello (World)";
Console.WriteLine($"{validString} --> {validString.IsValid()}");
Console.WriteLine($"{invalidString} --> {invalidString.IsValid()}");
}
The code above produces this result:
