Mesibo Phone Contacts Validation and Formatting
mesibo simplifies the process of validating, correcting, and formatting phone numbers to a great extent. mesibo's phone APIs offer faster performance compared to other APIs like libphonenumber
while maintaining the same or even better accuracy and reducing unnecessary overhead.
You can utilize the getPhoneNumberInfo()
function to validate and format phone numbers. It will also attempt to detect and add the country code if it's missing. This function returns a MesiboPhoneContact
object that contains various fields such as name, country, and type, as described below.
Here is an example of how to use it:
MesiboPhoneContacts contact = phoneManager.getPhoneNumberInfo(phoneNumber, true);
// access phone information if the phone number is valid
if(contact.valid) {
int type = contact.type; // Type of phone number (mobile, landline, etc)
String name = contact.name; // Name from the phone book
String phoneNumber = contact.phoneNumber; // validated and corrected phone number
String country = country.country; //Country name
...
}
getPhoneNumberInfo function
The getPhoneNumberInfo
function can be used in the following ways:
MesiboPhoneContact getPhoneNumberInfo(String phoneNumber);
MesiboPhoneContact getPhoneNumberInfo(String phoneNumber, boolean format);
MesiboPhoneContact getPhoneNumberInfo(String phoneNumber, String countryCode, boolean format);
getPhoneNumberInfo
takes the following parameters:
Parameter | Description |
---|---|
phoneNumber | The phone number to be parsed, validated, and formatted, which can be in any format (e.g., "1 (650) 555-1234", "6505551234", etc). mesibo will remove any unwanted characters before parsing. mesibo is also compliant to RFC 3966 |
format | true to format the output phone number, false otherwise. |
countryCode | ISO country code (e.g., "US") or phone code (e.g., "1"). This option overrides the default country code and disables automatic country detection. |
For example,
MesiboPhoneContacts contact = phoneManager.getPhoneNumberInfo(phoneNumber, "MX", true);
or
MesiboPhoneContacts contact = phoneManager.getPhoneNumberInfo(phoneNumber, "1", true);
MesiboPhoneContact class members
Following are the members of the MesiboPhoneContact class. Note that, fields may not be populated if the phone number is not valid.
member | Description |
---|---|
phoneNumber | Phone number in E.164 international format without + sign, for example, 16505551234 |
nationalNumber | Phone number in the national format without country code, for example, 6505551234 |
formattedPhoneNumber | formatted Phone number. You must pass the format parameter as ‘true` to get the formatted phone number, for example, +1 (650) 555-1234 |
country | Country name, for example, "United States" |
countryIsoCode | Country ISO code, for example, "US" |
countryPhoneCode | Country phone code, for example, "+1" |
name | Name from the phone book. You must call MesiboPhoneContactsManager.start() to scan and get the name |
profile | MesiboProfile object if the phone number is an app user and the profile has been synchronized |
valid | 'trueif the number is valid, false` otherwise |
possiblyValid | 'trueif the number is valid or may be valid but not invalid, false` otherwise |
type | Type of the phone number, MOBILE, FIXED, TOLLFREE, VOIP, etc. |
Setting Country Code
The mesibo phone contact API uses multiple algorithms to detect the country code on mobiles with high accuracy. You can further improve accuracy by providing the user's phone country code (e.g. 1 for USA and Canada, 65 for Singapore).
For example, using country phone code:
MesiboPhoneContacts country = phoneManager.setCountryCode("1");
or, by using country iso code
MesiboPhoneContacts country = phoneManager.setCountryCode("SG");
Alternatively, you can use the user's phone number to set the country code. mesibo extracts the country code automatically:
MesiboPhoneContacts country = phoneManager.setCountryCodeFromPhoneNumber("18005551234");
or,
MesiboPhoneContacts country = phoneManager.setCountryCodeFromPhoneNumber("8005551234", "US");
All the above functions return MesiboPhoneContacts
which you can examine to know the details of the country set.
It is important to note that inferring the country code from ISO country code, device locale, timezone, etc., is not always reliable. On Android, mesibo uses the SIM mobile country code (MCC) as one of the methods to detect the country code, but this capability is no longer available on iOS.
Phone number validation options
You can configure phone number validation by calling these functions:
phoneManager.ignoreInvalidCharacters(true);
The complete list of functions are below:
Function | Description | Default |
---|---|---|
ignoreInvalidCharacters | Ignore and strip invalid characters | true |
ignoreInvalidLength | Skip length validation | false |
truncateLongNumbers | If a number is determined to be valid but fails the length check, truncate it to make it valid | true |
detectMissingCountryCode | Detect missing country code in the phone number | true |
In the next section, we will describe phone contact monitoring and synchronization.