5

Background

It is possible to check if a given string represents a valid phone number, using this code, together with the PhoneNumberUtil library by Google (available here) :

public static boolean isValidNumber(final PhoneNumber phone) {
    if (phone == null)
        return false;
    final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    return phoneNumberUtil.isValidNumber(phone);
}

public static boolean isValidPhoneNumber(String phone) {
    if (TextUtils.isEmpty(phone))
        return false;
    phone = phone.replaceAll("[^0-9]", "");
    final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    PhoneNumber phoneNumber = null;
    try {
        phoneNumber = phoneNumberUtil.parse(phone, Locale.getDefault().getCountry());
    } catch (final Exception e) {
    }
    return isValidNumber(phoneNumber);
}

The problem

This is only a general check, but I can't find out if it's possible to know if the phone number is actually of a mobile device.

What I've tried

As I've read in some websites, the above check might need to have just a small adjustment to know if it's of a mobile device, but according to my tests, it's wrong :

    ...
    phone = phone.replaceAll("[^0-9]", "");
    if (phone.length() < 7)
        return false;
    ...

The question

Is it possible to know if the phone number is of a mobile device?

If so, how ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

3 Answers3

12

Seems I've missed the function in the library. Here's how to do it:

public static boolean isValidMobileNumber(String phone) {
    if (TextUtils.isEmpty(phone))
        return false;
    final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    try {
        PhoneNumber phoneNumber = phoneNumberUtil.parse(phone, Locale.getDefault().getCountry());
        PhoneNumberUtil.PhoneNumberType phoneNumberType = phoneNumberUtil.getNumberType(phoneNumber);
        return phoneNumberType == PhoneNumberUtil.PhoneNumberType.MOBILE;
    } catch (final Exception e) {
    }
    return false;
}

EDIT: it seems some numbers (in the US and a few others ) cannot be determined whether they are of mobile phones or not. In this case, FIXED_LINE_OR_MOBILE is returned.

If you still need to know if the phone number is mobile, there are online services to check it out (though they can't know all, of course), such as Pathfinder.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

This is the mobile phone validation for Turkey. You can arrange the regex as your country.You can check the algorithm from Wikipedia

public static boolean validateMobilePhone(String phone) {
  String regEx = "(^(\\+905)|^(905)|^(05)|^(5))((01)|(05)|(06)|(07)|(51)|(52)| 
  (53)|(54)|(55)|(59)|(30)|(31)|(32)|(33)|(34)|(35)|(36)|(37)|(38)|(39)|(61)| 
  (40)|(41)|(42)|(43)|(44)|(45)|(46)|(47)|(48)|(49))(\\d{7})$";
    
 return Pattern.compile(regEx).matcher(phone).matches();
}
ÖMER TAŞCI
  • 546
  • 5
  • 9
-1

you can do it if you want for one country regular expression

   private boolean isValidNumber(String phonenumber) {
            String PHONE_PATTERN = "^(984|986)\\d{7}$"; 
// phone number starts with 984 and 986 and has 7 digit after that ex: 9842248633 (true or valid) and 9851048633(false or not valid) and in total 10 digits number

            Pattern pattern = Pattern.compile(PHONE_PATTERN);
            Matcher matcher = pattern.matcher(phonenumber);
            return matcher.matches();
        }

And you can call this function like this:

phoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!isValidNumber(s.toString())){
                    isnumbervalid=false;
                    phoneNumber.setTextColor(Color.parseColor("#F44336"));
                }else {
                    isnumbervalid=true;
                    phoneNumber.setTextColor(Color.parseColor("#000000"));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

And further more you can also limit android:maxlength=10; in edit text to not let user enter number with lenght greater than 10

If you need for Country or Locale basis then you can use this using Google phonenumber library.

Dependency for this library if you are using gradel

compile 'com.googlecode.libphonenumber:libphonenumber:7.1.1'





String nepalNumberStr = "9842248633";
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE); // for device containing sim card
        String locale = tm.getNetworkCountryIso();// for device containing sim card
        // Else you need to access locale through GPS
        try {
            Phonenumber.PhoneNumber nepalNumber = phoneUtil.parse(nepalNumberStr, locale.toUpperCase());
            boolean isValid = phoneUtil.isValidNumber(nepalNumber); // returns true
            if(isValid) {
                String internationally = phoneUtil.format(nepalNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
                Toast.makeText(this, nepalNumberStr + " is " + isValid + " and number is :" + internationally + "And is "+ phoneUtil.getNumberType(nepalNumber), Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "This phone number is not valid for " + locale, Toast.LENGTH_SHORT).show();
            }
        } catch (NumberParseException e) {
            System.err.println("NumberParseException was thrown: " + e.toString());
        }

OK please Check this line out for MOBILE or FIXED_LINE phoneUtil.getNumberType(nepalNumber)

taman neupane
  • 938
  • 9
  • 18
  • Again, this isn't about general phone validation. It's about checking if it's a mobile phone number or not. – android developer Jan 19 '17 at 09:24
  • for which region or country you are targeting app ? – taman neupane Jan 19 '17 at 09:25
  • yes it can be done according to http://libphonenumber.appspot.com/ . we need to input phone number and country (ISO 3166-1 two-letter country code) . you have the phone number and you can get locale code using String locale = context.getResources().getConfiguration().locale.getCountry(); // let me check and come back to you since then you can alos try it. – taman neupane Jan 19 '17 at 09:48
  • I asked about checking if the given phone number is of a mobile phone, or not. I didn't ask about if it's valid or not. Also, I've already written the answer using this library. – android developer Jan 19 '17 at 11:09
  • Yes now you can check if it is MOBILE or FIXED_LINE – taman neupane Jan 19 '17 at 11:17
  • Yes that's what I've found – android developer Jan 19 '17 at 11:27