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!
-
Create a regular expression that validates NOT a PO BOX or use custom validator.
ccook : I agree, this would be ideal. But inverting an existing regex is non trivial?Alex Reitbort : It depends on the regexp itself, but as far as I know there is no magic 'not' switch on the whole expression.ccook : 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. -
Just use NegativeRegularExpressionValidator :)
[ToolboxData("<{0}:NegativeRegularExpressionValidator runat=\"server\" ErrorMessage=\"NegativeRegularExpressionValidator\"></{0}:NegativeRegularExpressionValidator>")] public class NegativeRegularExpressionValidator : RegularExpressionValidator { protected override bool EvaluateIsValid() { return base.EvaluateIsValid() == false; } }
ccook : Nice, I am going to give it a shot. tyCanavar : +1, but what about client side validation ?Mike Chaliy : Unfortunately, nothing about client side :(. Thank you for pointing this out. My aim was to give an idea, not more. -
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.
-
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+$).*$