views:

84

answers:

1

While working on a huge form for a client I realized that the majority of the regex validators I had were for the same regex:

^[^<>]*$

It's an easy way to prevent HTML entry into of TextBox controls.

I was curious if anyone else had a regex that they used more often or if there's one that I should be using instead of this one.

A: 

I think this approach is a little extreme. ASP.NET has validateRequest on by default in Machine.config:

<system.web>
  <pages buffer="true" validateRequest="true" />
</system.web>

Of course you can always set this yourself for the web app and it can be set at the page level as well:

<%@ Page Language="C#" ValidateRequest="true" %>

This prevents potentially harmful tags from being entered. For more details check out:

That's not to say that you shouldn't verify input, but I don't think it's appropriate to slap a regex validator on every single textbox control.

EDIT: if you're interested, it's possible to handle the ValidateRequest error to provide a friendlier message and keep the user on the same page (not just replace it with a custom error page). For more info, check out Kirk Evans' post: Handling ValidateRequest errors within a Page (refer to the section titled Overriding the OnError Method).

Ahmad Mageed
Actually I do have validateRequest set to true by default. The regex validator will display a much friendlier message than an error message would though.
travis
@travis: that's true. Take a look at my edit for a possible approach.
Ahmad Mageed