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!
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.
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'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+$).*$