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.
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.
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");
}
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