Requirements for a TextBox control were to accept the following as valid inputs:
- A sequence of numbers.
- Literal string 'Number of rooms'.
- No value at all (left blank). Not specifying a value at all should allow for the RegularExpressionValidator to pass.
Following RegEx yielded the desired results (successfully validated the 3 types of inputs):
"Number of rooms|[0-9]*"
However, I couldn't come up with an explanation when a colleague asked why the following fails to validate when the string 'Number of rooms' is specified (requirement #2):
"[0-9]*|Number of rooms"
An explanation as to why the ordering of alternation matters in this case would be very insightful indeed.
UPDATE:
The second regex successfully matches the target string "Number of rooms" in console app as shown here. However, using the identical expression in aspx markup doesn't match when the input is "Number of rooms". Here's the relevant aspx markup:
<asp:TextBox runat="server" ID="textbox1" >
</asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
EnableClientScript="false" runat="server" ControlToValidate="textbox1"
ValidationExpression="[0-9]*|Number of rooms"
ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />