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
Solved

Urgent: Phone Number Regular Expression Validation

Posted on 2014-01-01
8
353 Views
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!
0
Comment
Question by:wintersun
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

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

Open in new window

0
 

Author Comment

by:wintersun
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.

56100
07-5767880
713-434-7798
+6423412555

Please advise.
0
 
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  ; )
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:wintersun
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
[\d-()+\s]*{,15}

Any thoughts?
Thanks.
0
 
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

0
 

Author Comment

by:wintersun
ID: 39750830
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?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 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.
0
 

Author Closing Comment

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

Above is working in my case after testing.

Thanks.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

809 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