skip to Main Content

As a part of developing a mobile app development, we get access to the user’s contact list. Then we need to filter the contacts with a cell number (mobile). In other words, we need to exclude contacts with landline numbers. The question is how we can do this for all countries?

We initially thought that in all countries the mobile numbers are 10 digits starting with zero (0 xxx xxx xxx). But it looks like that’s not the case.

Apps like WhatsApp or Telegram are doing this. For example, once you open contacts in WhatsApp, it will only show your contacts with a mobile number. So there must be a way to do this.

We need a global solution for this matter.

2

Answers


  1. A 10 digit number is only valid for some countries, a mobile number can vary from 7 to 13 digit, you can use for two methods which I would have used, one would be elimination, check for negative case and return it, i.e. check for characters, special character, length between 7 to 13, if it validate, then we’ll assume it is a mobile number, or use the android Phone matcher functions.

    private boolean validateMobileNumber(String phone) {
    return android.util.Patterns.PHONE.matcher(phone).matches();    
    }
    

    or

    private boolean validateMobileNumber(String mobileNumber) {
        if(!Pattern.matches("[a-zA-Z]+", phone)) {
            return mobileNumber.length() > 6 && mobileNumber.length() <= 13;
        }
        return false;
    }
    
    Login or Signup to reply.
  2. As you considered, your assumption is totally incorrect.
    Every country has its own zip code and has different number of digits for its phone numbers.

    For parsing international phone number I suggest you an awesome library that I used it a lot PhoneNumberKit

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search