libPhoneNumber
libphoneNumber Library is used to validate the phone number and it is a google's library mostly used by google products and Whatsapp
phone number validation is important for app so that you can get valid number from your users. Any verification code, messages that you want to send them will have less chances of being missed.
Basic requirements
- Download the libPhoneNumber jar and add this jar as a library in your app.
- Get the country ISO code
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String countryCode = tm.getSimCountryIso();
or you can visit link : http://www.iso.org/iso/country_names_and_code_elements
PhoneNumberUtil phoneUtil = PhoneNumberUtil
.getInstance();
Phonenumber.PhoneNumber phNumberProto = null;
try {
// I set the default region to IN (India)
// You can find your country code here
phNumberProto = phoneUtil.parse("9411532876", "IN"); //replace Iso code here
} catch (NumberParseException e) {
// if there's any error
System.err
.println("NumberParseException was thrown: "
+ e.toString());
}
// check if the number is valid
boolean isValid = phoneUtil
.isValidNumber(phNumberProto);
if (isValid) {
// get the valid number's international format
String internationalFormat = phoneUtil.format(
phNumberProto,
PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
Toast.makeText(
getBaseContext(),
"Phone number VALID: " + internationalFormat,
Toast.LENGTH_SHORT).show();
} else {
// prompt the user when the number is invalid
Toast.makeText(
getBaseContext(),
"Phone number is INVALID: " + phoneNumber,
Toast.LENGTH_SHORT).show();
}
break;
}
}
};
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Attach jar is older version but it will work.
1 Comment(s)