Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Urgent: Phone Number Regular Expression Validation

Posted on 2014-01-01
Medium Priority
Last Modified: 2014-01-02
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!
Question by:wintersun
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39750689
String pattern = "[\\d-0+]{,15}";

Open in new window


Author Comment

ID: 39750729
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.


Please advise.
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39750744
Remove the backslashes before each curly brace. I didn't not put those in my pattern for a reason  ; )
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 39750782
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

Any thoughts?
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39750788
Take the star out. Also, I need to ammend the pattern slightly:

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

Open in new window


Author Comment

ID: 39750830
Still having the same error returned:-

java.util.regex.PatternSyntaxException: Illegal repetition near index 7
      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?
LVL 75

Accepted Solution

käµfm³d   👽 earned 1500 total points
ID: 39750832
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.

Author Closing Comment

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

Above is working in my case after testing.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question