Skip to content

Instantly share code, notes, and snippets.

@jdowd7
Created October 30, 2015 18:03
Show Gist options
  • Save jdowd7/e611dcea6482aac1727f to your computer and use it in GitHub Desktop.
Save jdowd7/e611dcea6482aac1727f to your computer and use it in GitHub Desktop.
Checks to see if a string contains a special character
public static ValidationResult ContainsSpecChar(string sampleStr)
{
Regex specChar = new Regex(@"[^A-Za-z0-9!?.&,\ -]", RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = specChar.Matches(sampleStr);
if (matches.Count > 0)
{
return new ValidationResult("Message cannot contain special characters.");
}
return ValidationResult.Success;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment