See If You Can Modify This Simple Regular Expression

  • Thread starter Thread starter Joey
  • Start date Start date
J

Joey

I currently have a regular expression that will match textbox input for
phone numbers formatted as ###-###-####.

The expression is...

\d{3}-\d{3}-\d{4}

How can I modify this to accept the same pattern (ten digits) OR
###-#### (seven digits)?
 
Joey said:
I currently have a regular expression that will match textbox input for
phone numbers formatted as ###-###-####.

The expression is...

\d{3}-\d{3}-\d{4}

How can I modify this to accept the same pattern (ten digits) OR
###-#### (seven digits)?

1) This will match a telephone number anywhere in the input. If you're
scanning a body of text for phone numbers, use it. If you're validating
user input, add ^ to the start and $ to the end...

2) In answer to your question:
(\d{3})?-\d{3}-\d{4}

or with both:
^(\d{3}-)?\d{3}-\d{4}$

Damien
 
Joey said:
I currently have a regular expression that will match textbox input for
phone numbers formatted as ###-###-####.

The expression is...

\d{3}-\d{3}-\d{4}

How can I modify this to accept the same pattern (ten digits) OR
###-#### (seven digits)?
The | is used for "or" so it would be something like
\d{3}-\d{3}-\d{4}|\d{3}-\d{4}
 
Back
Top