-8

I have to validate a phone number based on the following criteria,

It should only take numeric values.
Minimum 10 and Maximum 15

How can I write a regular expression in java satisfying the above? I am new to regular expressions.

  • 1
    Share what have you tried with us. SO is for seeking help, not getting your work done. – Apurv May 03 '13 at 05:03
  • 1
    do you have a specific format for phone number ? Why not simply `^\d{10,15}$` – NeverHopeless May 03 '13 at 05:03
  • 2
    I hope you realize that phone numbers according to the ITU recommendations can have spaces and other characters. Particularly they can start with a '+' (to represent the international access code that differs per country you are calling from). Whoever setup the criteria has to get around the globe some more. – Anthon May 03 '13 at 05:25
  • 1
    and may have more or fewer numbers to be legal depending on locality and scope (e.g. where I live a number would be 7 long if dialed locally, 10 nationally, 12 internationally, and mobile numbers different again). – jwenting May 03 '13 at 05:40

3 Answers3

3

Try the regular expression

^\\d{10,15}$

Here \d is a predefined character class for digits
{10, 15} quantifier stands for a repeat of 10 to 15 times of the previous pattern

Ex:

String input = "1234567890";
Pattern pattern = Pattern.compile("^\\d{10,15}$");
if (pattern.matcher(input).find()) {
    System.out.println("Valid");
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Use this regex:

\\d{10,15}

\d matches a digit (preceding \ is for escaping)

{10,15} allows minimum 10 and maximum 15 occurrence of preceding pattern

Naveed S
  • 5,106
  • 4
  • 34
  • 52
0

\d is for number 0-9

(\d){10,15}
marcadian
  • 2,608
  • 13
  • 20