Phone number validation Android

2019-01-10 16:45发布

How do I check if a phone number is valid or not? It is up to length 13 (including character + in front).

How do I do that?

I tried this:

String regexStr = "^[0-9]$";

String number=entered_number.getText().toString();  

if(entered_number.getText().toString().length()<10 || number.length()>13 || number.matches(regexStr)==false  ) {
    Toast.makeText(MyDialog.this,"Please enter "+"\n"+" valid phone number",Toast.LENGTH_SHORT).show();
    // am_checked=0;
}`

And I also tried this:

public boolean isValidPhoneNumber(String number)
{
     for (char c : number.toCharArray())
     {
         if (!VALID_CHARS.contains(c))
         {
            return false;
         }
     }
     // All characters were valid
     return true;
}

Both are not working.

Input type: + sign to be accepted and from 0-9 numbers and length b/w 10-13 and should not accept other characters

12条回答
相关推荐>>
2楼-- · 2019-01-10 17:17

You can use PhoneNumberUtils if your phone format is one of the described formats. If none of the utility function match your needs, use regular experssions.

查看更多
成全新的幸福
3楼-- · 2019-01-10 17:19

you can also check validation of phone number as

     /**
     * Validation of Phone Number
     */
    public final static boolean isValidPhoneNumber(CharSequence target) {
        if (target == null || target.length() < 6 || target.length() > 13) {
            return false;
        } else {
            return android.util.Patterns.PHONE.matcher(target).matches();
        }

    }
查看更多
爷、活的狠高调
4楼-- · 2019-01-10 17:22

Given the rules you specified:

upto length 13 and including character + infront.

(and also incorporating the min length of 10 in your code)

You're going to want a regex that looks like this:

^\+[0-9]{10,13}$

With the min and max lengths encoded in the regex, you can drop those conditions from your if() block.

Off topic: I'd suggest that a range of 10 - 13 is too limiting for an international phone number field; you're almost certain to find valid numbers that are both longer and shorter than this. I'd suggest a range of 8 - 20 to be safe.

[EDIT] OP states the above regex doesn't work due to the escape sequence. Not sure why, but an alternative would be:

^[+][0-9]{10,13}$

[EDIT 2] OP now adds that the + sign should be optional. In this case, the regex needs a question mark after the +, so the example above would now look like this:

^[+]?[0-9]{10,13}$
查看更多
冷血范
5楼-- · 2019-01-10 17:22

To validate phone numbers for a specific region in Android, use libPhoneNumber from Google, and the following code as an example:

public boolean isPhoneNumberValid(String phoneNumber, String countryCode)
{
    //NOTE: This should probably be a member variable.
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();

    try 
    {
        PhoneNumber numberProto = phoneUtil.parse(phoneNumber, countryCode);
        return phoneUtil.isValidNumber(numberProto);
    } 
    catch (NumberParseException e) 
    {
        System.err.println("NumberParseException was thrown: " + e.toString());
    }

    return false;
}
查看更多
干净又极端
6楼-- · 2019-01-10 17:23

Use isGlobalPhoneNumber() method of PhoneNumberUtils to detect whether a number is valid phone number or not.

Example

System.out.println("....g1..."+PhoneNumberUtils.isGlobalPhoneNumber("+912012185234"));
System.out.println("....g2..."+PhoneNumberUtils.isGlobalPhoneNumber("120121852f4"));

The result of first print statement is true while the result of second is false because the second phone number contains f.

查看更多
贪生不怕死
7楼-- · 2019-01-10 17:23

You can use android's inbuilt Patterns:

public boolean validCellPhone(String number)
{
    return android.util.Patterns.PHONE.matcher(number).matches();
}

This pattern is intended for searching for things that look like they might be phone numbers in arbitrary text, not for validating whether something is in fact a phone number. It will miss many things that are legitimate phone numbers.

The pattern matches the following:

  • Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow.
  • Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
  • A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
查看更多
登录 后发表回答