1

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

4
  • Thank you I replaced the expression with the one you suggested: ValidationExpression="(\d{3}-\d{3}-\d{4})|\d{6,9}" but the problem remains. Commented Mar 24, 2020 at 10:35
  • The code: ValidationExpression="(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}?" works properly but only if I enter 999-999-9999 Thank you Commented Mar 24, 2020 at 10:38
  • colinD: Yes I saw it and used it. But it doesn't work. I don't know what to say. Thank you Commented Mar 24, 2020 at 11:01
  • colinD: I'm sorry I don't speak well in English Commented Apr 1, 2020 at 8:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.