简体   繁体   中英

check mobile number valid or not through country code

I have contact like "+919672525253".Now i extract the country code like "91" from that number.Now if number is like "9672525253" and if i extract the country code then it will give me "967".So after extracting the country code how can i check that remaining number is valid mobile number for that country code or not? EDIT

If any body know the mobile number length country wise then also i can solve this problem.like in india 10 digits.

You pretty much can't. For example in the US mobile numbers and landline numbers are indistinguishable, they have normal area codes just like landline numbers. Even if it were possible every country does it differently and it is also constantly changing as numbers run out new prefixes are added and things change and their is no pattern you could match against or database you could do a lookup against.

Take a look at libPhoneNumber (bundled in ICS) which can help validating a phone number (see PhoneNumberUtils). There's a MobileType you can get after validation but as stated in the source and by Ben, in some region this will not work.


EDIT:

Some validation code (here we need to check the phone is a valid one assuming it's a french one):

boolean isValid = false;
PhoneNumber number = null;
try {
    number = this.phoneUtil.parse(phone, "FR"); // phone is number in internationnal format "+xxxxxx"
    isValid = this.phoneUtil.isValidNumber(number);
} catch (final NumberParseException e) {
    // ...
}

isValid // is the phone number valid according to the library?
this.phoneUtil.getRegionCodeForNumber(number); // this gets the country code of the phone as found by the library (for example "US", "CH", "GB", ...)

This works for us but you'll need to try it to see if it suit your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM