Search code examples
asp.netvalidationreg-expressionvalidator

Adding exception to RegularExpressionValidator


I am trying to add an exception to my RegularExpressionValidator

Basically I am populating a textbox with the text "Not supplied" if the user hasn't given us the information if they try and submit that field I have a RegularExpressionValidator which only allows 0-9 characters so it shows the error message.

Is there a way to add the exception "Not supplied" to my RegularExpressionValidator?

Here's the code for the RegularExpressionValidator

<asp:TextBox ID="tbEditFlightTime" CssClass="tbEditFlightTime" Visible="false" Width="100" runat="server"></asp:TextBox> 

<asp:RegularExpressionValidator ID="revFlightTime" runat="server" Display="Dynamic" ErrorMessage="<strong>Error</strong>" ControlToValidate="tbEditFlightTime" ValidationExpression="^[0-9]{4,4}$" SetFocusOnError="true" />

Thanks


Solution

  • You can use an alternation:

    <asp:RegularExpressionValidator ID="revFlightTime" runat="server"
        Display="Dynamic" ErrorMessage="<strong>Error</strong>"
        ControlToValidate="tbEditFlightTime"
        ValidationExpression="^(Not supplied|[0-9]{4,4})$"
        SetFocusOnError="true" />