How to validate that a string doesn't contain HTML using C#

The following will match any matching set of tags. i.e. <b>this</b>

Regex tagRegex = new Regex(@"<\s*([^ >]+)[^>]*>.*?<\s*/\s*\1\s*>");

The following will match any single tag. i.e. <b> (it doesn't have to be closed).

Regex tagRegex = new Regex(@"<[^>]+>");

You can then use it like so

bool hasTags = tagRegex.IsMatch(myString);

You could ensure plain text by encoding the input using HttpUtility.HtmlEncode.

In fact, depending on how strict you want the check to be, you could use it to determine if the string contains HTML:

bool containsHTML = (myString != HttpUtility.HtmlEncode(myString));

Here you go:

using System.Text.RegularExpressions;
private bool ContainsHTML(string checkString)
{
  return Regex.IsMatch(checkString, "<(.|\n)*?>");
}

That is the simplest way, since items in brackets are unlikely to occur naturally.