See If You Can Modify This Simple Regular Expression

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)?
 
D

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)?

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
 
V

vMike

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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top