How to check phone number format is valid or not from telephony manager?

To check the phone number (I have the model phone number like +918172014908). I am using libphonenumber.jar file. It checks the phone number depending on the country, valid or not. I am using this: -

 PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 
         PhoneNumber numberProto = phoneUtil.parse("phone_number", "");  
        phoneUtil.isValidNumber(numberProto) == true ? "valid" : "phone no not valid"

      

Works great ... But this jar file takes up little memory. Is there any other way to check if the phone number is formatted correctly without libphonenumber.jar ??? can you suggest something ???

+3


source to share


4 answers


This answer might help you: fooobar.com/questions/1811853 / ...

To check a string use

if (setNum.matches(regexStr))
where regexStr can 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's a very long regex for checking US phones (7 to 10 digits, permissions, etc.). Source from this answer: Comprehensive regex to validate 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+))?$"

      

+8


source


Try the following:



public static boolean isValidPhoneNo(CharSequence iPhoneNo) {
    return !TextUtils.isEmpty(iPhoneNo) &&
         Patterns.PHONE.matcher(iPhoneNo).matches();
}

      

+3


source


You can just use simple regex

. Let's say my phone number format is012-1234567

You can use \\d{3}-\\d{7}

to check them.

For example:

String number = "012-1234567";
Pattern pattern = Pattern.compile("\\d{3}-\\d{7}");
Matcher matcher = pattern.matcher(number); 
 if (matcher.matches()) {
      System.out.println("Phone Number Valid");
 }

      

0


source


Try the following:

/**
 * This method is used to set filter type of us phone number.
 * @param phone
 */
 public static void setFilterTypeOfUSPhoneNumber(final TextView phone){

        InputFilter filter = new InputFilter() { 
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
                    String pattern = "0123456789()- ";
                    for (int i = start; i < end; i++) { 
                            if (pattern.indexOf(source.charAt(i)) < 0 ||
                                    source.length() > 14) { 
                                    return ""; 
                            } 
                    } 
                    return null; 
            } 
        }; 

        phone.setFilters(new InputFilter[]{filter ,new InputFilter.LengthFilter(14)});
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void afterTextChanged(Editable s) {

                if(s.length() > 1){

                    if(s.length() < 5){
                        if(s.toString().indexOf("(") != 0 ||
                                checkSpecialCharsPositions(s.toString())){
                            String str = s.toString();
                            str = replaceStrings(str);
                            str = "("+str;
                            s.clear();
                            s.append(str);
                            phone.setText(s);

                        }
                    }
                    else if(s.length() < 10){
                        if(s.toString().indexOf("(") != 0 ||
                                s.toString().indexOf(")") != 4 ||
                                checkSpecialCharsPositions(s.toString())){
                            String str = s.toString();
                            str = replaceStrings(str);
                            str = "("+str.substring(0, 3)+") "+str.substring(3);
                            s.clear();
                            s.append(str);
                            phone.setText(s);
                        }
                    }
                    else {
                        if(s.toString().indexOf("(") != 0 ||
                                s.toString().indexOf(")") != 4 ||
                                s.toString().indexOf("-") != 9 ||
                                checkSpecialCharsPositions(s.toString())){

                            String str = s.toString();
                            str = replaceStrings(str);
                            str = "("+str.substring(0, 3)+") "+str.substring(3,6) + "-" + str.substring(6);
                            s.clear();
                            s.append(str);
                            phone.setText(s);
                        }
                    }
                }
                Selection.setSelection(s,s.length());
            }

            private String replaceStrings(String str){
                str = str.replace("(", "");
                str = str.replace(")", "");
                str = str.replace(" ", "");
                str = str.replace("-", "");
                return str;
            }

            private boolean checkSpecialCharsPositions(String str){

                return (str.indexOf("(") != str.lastIndexOf("(") ||
                        str.indexOf(")") != str.lastIndexOf(")") ||
                        str.indexOf("-") != str.lastIndexOf("-"));
            }
        });

    }

      

0


source







All Articles