Urgent: Phone Number Regular Expression Validation

I need a regular expression to limit digits for phone number fields to no more than 15 digits and also allow the special characters listed below:-

-
()
+

Can someone show me how to do it in regular expressions?

Thanks & Happy New Year!
wintersunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
String pattern = "[\\d-0+]{,15}";

Open in new window

wintersunAuthor Commented:
I try to test it on my Java application with the code below:-

private static final Pattern phonePattern = Pattern.compile("[\\d-()+]\\{,15\\}");


However, the input (listed below) failed to pass the pattern.

56100
07-5767880
713-434-7798
+6423412555

Please advise.
kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
Remove the backslashes before each curly brace. I didn't not put those in my pattern for a reason  ; )
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

wintersunAuthor Commented:
If I remove the backslashes before each curly brace, the following runtime error will be appeared:-


java.util.regex.PatternSyntaxException: Illegal repetition near index 10
[\d-()+\s]*{,15}

Any thoughts?
Thanks.
kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
Take the star out. Also, I need to ammend the pattern slightly:

String pattern = "[\\d()+-]{,15}";

Open in new window

wintersunAuthor Commented:
Still having the same error returned:-

java.util.regex.PatternSyntaxException: Illegal repetition near index 7
[\d()+-]{,15}
       ^
      at java.util.regex.Pattern.error(Pattern.java:1924)
      at java.util.regex.Pattern.closure(Pattern.java:3104)
      at java.util.regex.Pattern.sequence(Pattern.java:2101)
      at java.util.regex.Pattern.expr(Pattern.java:1964)
      at java.util.regex.Pattern.compile(Pattern.java:1665)
      at java.util.regex.Pattern.<init>(Pattern.java:1337)
      at java.util.regex.Pattern.compile(Pattern.java:1022)

What do you think?
kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
You might need to explicitly declare the zero:

String pattern = "[\\d()+-]{0,15}";

Open in new window


Or use a 1 if you need at least one item.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wintersunAuthor Commented:
private static final Pattern phonePattern = Pattern.compile("[\\d-()+\\s]{6,15}");

Above is working in my case after testing.

Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.