
i want a number in a specific range in pattern matcher..for example a number should only be in the range of 6-9 digits...if it exceeds or decrease it should show an error.Here is the code which i am trying...i this it checks for 10 digits...but i want it in the range of 6-9
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class ValidatePhoneNumber { public static void main(String[] argv) {
String sPhoneNumber = tf.getText();
Pattern pattern = Pattern.compile("\\d{10}");
Matcher matcher = pattern.matcher(sPhoneNumber);
if (matcher.matches()) {
System.out.println("Phone Number Valid");
}
else
{
System.out.println("Phone Number not valid");
}
} }

import java.io.*;
import java.util.regex.*;
class pmatch
{
public static void main(String args[])throws IOException
{
Pattern p=Pattern.compile("[0-9]{9}|[0-9]{8}|[0-9]{7}|[0-9]{6}");
System.out.println("Enter no.");
BufferedReader br=new BufferedReader (newInputStreamReader(System.in));
String exp=br.readLine();
Matcher m=p.matcher(exp);
boolean b=m.matches();
if(b==true)
System.out.println("Valid number");
else
System.out.println("Invalid number");
}
}

Here is a code that validates phone number.
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidatePhoneNumber {
public static boolean validatePhone(String phone) {
return phone.matches("^[0-9]{6,9}$");
}
public static void main(String[] argv) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Phone Number: ");
String phone = input.nextLine();
while (validatePhone(phone) != true) {
System.out.print("Invalid PhoneNumber!");
System.out.println();
System.out.print("Enter Phone Number: ");
phone = input.nextLine();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.