Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PhoneNumberUtils isWellFormedSmsAddress

Tags:

android

What is a well formed sms address?

I am doing this in my code:

if (StringUtils.isNotBlank(mPhone) && !PhoneNumberUtils.isWellFormedSmsAddress(mPhone)) {
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(getString(R.string.cellphone_error));
    alertDialog.setMessage(getString(R.string.cellphone_error_hint));
    alertDialog.show();
    return;
}

However, I can't seem to input any set of numbers to activate this code. For some reason, everything seems to qualify as a well formed sms address. How does this error check work?

Should I just use isGlobalPhoneNumber instead?

like image 383
Jason Axelrod Avatar asked May 13 '26 20:05

Jason Axelrod


1 Answers

Bottom line after looking at either one of those methods you probably want to use your own RegEx to validate a number such as those used here depending on your requirements.

Reason being is that PhoneNumberUtils.isWellFormedSmsAddress(...) extracts the network part of the passed in String using PhoneNumberUtils.extractNetworkPortion(...) and basically checks if that network portion has dialable digits.

Problem is PhoneNumberUtils.extractNetworkPortion(...) removes most non digits so +18fvg12 becomes +1812 and still passes.

PhoneNumberUtils.isGlobalPhoneNumber (...) appears a bit more useful and makes sure the phone number matches the RegEx [\+]?[0-9.-]+.

This still might not be desirable though as +-----598-- still passes as an example.

like image 169
George Mulligan Avatar answered May 19 '26 02:05

George Mulligan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!