0

The Task it to check the input number is valid if it starts with only 7/8/9 and contains 10 digits. Here is the code. Correct me. I know I'm wrong.

public boolean validateMobileNumber(long number) throws Exception {

    String num = Long.toString(number);

    String regex = "@\"^[7-9]{10}$";
    if (num.equals(regex))
        return true;
    else
        throw new Exception("Invalid Number");
}
thibsc
  • 3,747
  • 2
  • 18
  • 38

1 Answers1

0

It's because you use equals to check the match of your regex. You will use matches

String num = "7123456789";
String regex = "^[7-9]\\d{9}$";

System.out.println(num.matches(regex));
thibsc
  • 3,747
  • 2
  • 18
  • 38