Reputation: 83
In an HTML DOC I reported for a textbox the following validation code:
<asp:RegularExpressionValidator ID="revPhone" runat="server"
CssClass="validator" Display="Dynamic" ErrorMessage="Phone number"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}?|(\d{6.9})?"
ControlToValidate="txtPhone" >Use this format: 999-999-9999 or 999999 0r 999999999
</asp:RegularExpressionValidator><br />
I would like to understand if this regular expression:
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}?|(\d{6.9})?"
is correct to be able to enter one of the following values:
234-456-7890
234567
234567890
Because it returns me the error message if I enter:
234567890
Upvotes: 1
Views: 619
Reputation: 2039
The start and end of the string delimiters are missing, which is why I thing the regex is not working.
^
is the start of the string, $
the end of it.
This should work:
^\d{3}-\d{3}-\d{4}$|^\d{6}$|^\d{9}$
Which means:
^\d{3}-\d{3}-\d{4}$ --> start, then 3 groups of digits with a '-' in the middle, then end
| --> or
^\d{6}$ --> start then 6 digits then end
| --> or
^\d{6}$ --> start then 9 digits then end
You can see it here: https://regex101.com/r/HgI3R5/2
Good website to test regex, includes reference of the tokens you can use and an explanation of the regex you're building.
Upvotes: 1