views:

537

answers:

3

I have a text box and need to validate that it is plain text does not contain html tags.

What is the best way to do it in .net

Bonus Question: How to do the same with javascript.

+1  A: 

This post provides a nice clean HTML removal method. It uses RegEx, so it could easily be adapted to JavaScript.

Andrew Van Slaars
what if I do not want to strip but just detect and give message to the user.
Salt Packets
To find out if it contains the tags without changing it, use Regex.Match instead of Regex.Replace.
Neil Whitaker
A: 

Why not escape/encode any html, you'll get 1001 different ways to detect html with RegEx's but most will be flawed in some way. If escaping is not an option then your best bet might be to put a RegEx tag on the question and into the title to attract some experts.

Dan Powley
A: 

You could allow it to be entered, then before doing ANYTHING with the input, you could use the HttpServerUtility.HtmlEncode class that will convert it all, so that when you re-render it (like in a comment), it wont be the original HTML, but rather the encoded rendering of it.

Mark