I have the following regex set as the ValidationExpression
property on a RegularExpressionValidator in a web form. When I enter an illegal character in the validated control, the validator detects it and shows an error message.
<appSettings>
<add key="categoryPattern" value="^[a-zA-Z0-9_+\-() ]{1,50}$" />
</appSettings>
My validator:
<asp:RegularExpressionValidator ValidationExpression="<%$ AppSettings:categoryPattern %>"
My server side validation:
Regex rex = new Regex(ConfigurationManager.AppSettings["categoryPattern"]);
if (!rex.Match(categoryName).Success)
{
throw new ArgumentException("CategoryName must match expression: " + rex);
As you can see, exactly the same pattern is applied client side and server side.
However, when I clear the validated control and submit an empty string, the validator thinks it's OK, and I get an error from my server side validation. Anyone know what is wrong here, except for the broken contract of the RegularExpressionValidator?