任务检查输入的数字,如果它仅以 7/8/9 开头并且包含 10 位数字,则该数字有效。 这是代码。 纠正我。我知道我错了。
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");
}
最佳答案
这是因为您使用equals
来检查正则表达式的匹配。您将使用 matches
String num = "7123456789";
String regex = "^[7-9]\\d{9}$";
System.out.println(num.matches(regex));
关于java - 需要手机号码验证指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61488731/