Check cell number on Android

I have a user interface in which the user enters a cell number and the number is stored in a string variable and then stored in the database.

I want to check a string to see if the string contains any characters.

How to do it?

Below is the line code.

EditText editText3 = (EditText) this.findViewById(R.id.editText2);
setNum = editText3.getText().toString(); //setNum would contain something link 8081124589
+3
source share
5 answers

To check a string, use

if (setNum.matches(regexStr))

where regexStr could be:

//matches numbers only
String regexStr = "^[0-9]*$"

//matches 10-digit numbers only
String regexStr = "^[0-9]{10}$"

//matches numbers and dashes, any order really.
String regexStr = "^[0-9\\-]*$"

//matches 9999999999, 1-999-999-9999 and 999-999-9999
String regexStr = "^(1\\-)?[0-9]{3}\\-?[0-9]{3}\\-?[0-9]{4}$"

There is a very long regular expression for checking phones in the USA (from 7 to 10 digits, permissions, etc.). Outcome from this answer: Full regex for checking phone number

String regexStr = "^(?:(?:\\+?1\\s*(?:[.-]\\s*)?)?(?:\\(\\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\\s*\\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\\s*(?:[.-]\\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\\s*(?:[.-]\\s*)?([0-9]{4})(?:\\s*(?:#|x\\.?|ext\\.?|extension)\\s*(\\d+))?$"
+25
source

For API level 8 or higher:

public static final boolean isValidPhoneNumber(CharSequence target) {
    if (target == null || TextUtils.isEmpty(target)) {
        return false;
    } else {
        return android.util.Patterns.PHONE.matcher(target).matches();
    }
}
+10
source

, {Integer.parseInt(setNum); keep go} catch ( e) {oops not number}

Double.parseDouble(setNum); , .

, .

, , , .

, , , edittext . xml android: digits = "0123456789-()" .

, , :

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
  mPhoneNumber = tMgr.getLine1Number();

- android: name= "android.permission.READ_PHONE_STATE".

, (. Android)

+1

PhoneNumberUtils. - formatNumber (String num, String defaultCountryIso). formatNumber(); null, . .

:

 EditText editText3 = (EditText) this.findViewById(R.id.editText2);
 String setNum;

 if(!TextUtils.isEmpty(editText3.getText()){
      setNum = editText3.getText.toString();

      /* format with PhoneNumberUtils */
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      String mCountryCode = telephonyManager.getNetworkCountryIso();

      // I like to check for sdk cause less than 21 won't support default country code
      if (android.os.Build.VERSION.SDK_INT >= 21) {
          setNum = PhoneNumberUtils.formatNumber(phoneNumber, mCountryCode);
       } else {
          // this one is deprecated
          setNum = PhoneNumberUtils.formatNumber(phoneNumber);
       }

      // if setNum is not null, you now have a valid formatted number
      if(setNum != null){
         return true;
      } else {
         editText3.setError("Invalid phone number");
         Log.d(TAG, "Invalid phone number: " + setNum); 
         return false;
      }
 } else {
    return false;
 }

, TelephonyManager [ ? ]. , :

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

+1

. , setError;

TextView v = (TextView) view;
setError(message);

, . BARACUS Framework Android, android.

Apache 2 , .

0

All Articles