tags:

views:

691

answers:

4

Is there a way to use the RegularExpressionValidator to validate only when the ValidationExpression does not match? In particular use a PO BOX regex to validate an address that is NOT a PO BOX.

Thanks!

+1  A: 

Create a regular expression that validates NOT a PO BOX or use custom validator.

Alex Reitbort
I agree, this would be ideal. But inverting an existing regex is non trivial?
ccook
It depends on the regexp itself, but as far as I know there is no magic 'not' switch on the whole expression.
Alex Reitbort
Right, characters can be negated but not the whole expression (as far as i can tell). In the mean time it's a custom validator.
ccook
+3  A: 

Just use NegativeRegularExpressionValidator :)

[ToolboxData("<{0}:NegativeRegularExpressionValidator runat=\"server\" ErrorMessage=\"NegativeRegularExpressionValidator\"></{0}:NegativeRegularExpressionValidator>")]
public class NegativeRegularExpressionValidator : RegularExpressionValidator
{
    protected override bool EvaluateIsValid()
    {
     return base.EvaluateIsValid() == false;
    }
}
Mike Chaliy
Nice, I am going to give it a shot. ty
ccook
+1, but what about client side validation ?
Canavar
Unfortunately, nothing about client side :(. Thank you for pointing this out. My aim was to give an idea, not more.
Mike Chaliy
+1  A: 

Mike Chaliy's solution is very good, but it doesn't validate in client-side.

I think you should use a custom validator control and write a javascript function that validates not a POBox. copy pattern from regular expression validator and use this site to write your own client side validation.

Canavar
+2  A: 

You can effectively invert your regular expression using a negative look-ahead.

For example, consider the following regular expression that matches only a string of digits:

^\d+$

This expression could be inverted as follows:

^(?!\d+$).*$
Nick Higgs