如何验证8到10位长的电话号码?

时间:2022-06-29 23:58:47

In my android application, phone number field used. 8 to 10 digit input value given by the user. How to validate 8 to 10 digit phone number using PHONE_REGEX?

在我的Android应用程序中,使用了电话号码字段。用户给出的8到10位输入值。如何使用PHONE_REGEX验证8到10位数的电话号码?

I have already tried this method,

我已经尝试过这种方法,

private static final String PHONE_REGEX = "\\d{8-10}";

  public static boolean isPhoneNumber(EditText editText, boolean required) {
    return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}

4 个解决方案

#1


1  

Use this

private static final String PHONE_REGEX = "\\d{8,10}";

public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}

instead of

private static final String PHONE_REGEX = "\\d{8-10}";

public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}

only one small mistake.Before ask a question,refer previous stack answers.

只有一个小错误。在提出问题之前,请参考之前的堆栈答案。

#2


1  

Let's try \\d{8,10} instead of \\d{8-10}

我们试试\\ d {8,10}而不是\\ d {8-10}

#3


1  

You can use this for validation.

您可以使用它进行验证。

    public static boolean isValidIndianMobile(String mobile) {

            if (mobile == null || mobile.trim().length() <= 0) {
                return false;
            }
            mobile = removeAllSpace(mobile);

            Pattern pattern = Pattern.compile("[7-9][0-9]{9}");
            Matcher matcher = pattern.matcher(mobile);
            return matcher.matches();
        }


   public static String removeAllSpace(String s) {
           if (s == null) return "";
           return s.replaceAll("\\s", "");
   }

#4


0  

Use \\d{8,10} instead of \\d{8-10}

使用\\ d {8,10}而不是\\ d {8-10}

and trim() the text

和trim()文本

private static final String PHONE_REGEX = "\\d{8,10}";

  public static boolean isPhoneNumber(EditText editText, boolean required) {
    return isValid(editText.getText().toString().trim(), PHONE_REGEX, PHONE_MSG, required);
}

#1


1  

Use this

private static final String PHONE_REGEX = "\\d{8,10}";

public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}

instead of

private static final String PHONE_REGEX = "\\d{8-10}";

public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}

only one small mistake.Before ask a question,refer previous stack answers.

只有一个小错误。在提出问题之前,请参考之前的堆栈答案。

#2


1  

Let's try \\d{8,10} instead of \\d{8-10}

我们试试\\ d {8,10}而不是\\ d {8-10}

#3


1  

You can use this for validation.

您可以使用它进行验证。

    public static boolean isValidIndianMobile(String mobile) {

            if (mobile == null || mobile.trim().length() <= 0) {
                return false;
            }
            mobile = removeAllSpace(mobile);

            Pattern pattern = Pattern.compile("[7-9][0-9]{9}");
            Matcher matcher = pattern.matcher(mobile);
            return matcher.matches();
        }


   public static String removeAllSpace(String s) {
           if (s == null) return "";
           return s.replaceAll("\\s", "");
   }

#4


0  

Use \\d{8,10} instead of \\d{8-10}

使用\\ d {8,10}而不是\\ d {8-10}

and trim() the text

和trim()文本

private static final String PHONE_REGEX = "\\d{8,10}";

  public static boolean isPhoneNumber(EditText editText, boolean required) {
    return isValid(editText.getText().toString().trim(), PHONE_REGEX, PHONE_MSG, required);
}