简体   繁体   中英

How to validate phone number in android application?

I am working on android. In my app users will register by entering their phone number in edittext. This phone number is then saved in database. Now when the user login with the app, I am getting the list of contacts from his mobile and comparing that with the people who register with this app. If the number in the contacts list matches with the number in the database, then I need to display those numbers in listview. Here the problem is if the user save his number with +91 or with 0 before his contact then the number in the database is not matching with the contact. At that time the numbers are not displaying.

For this issue, Do we need to keep any alert before entering the number in edit text? For example in edit text I gave, Ph no: 8923458128 and the saved it in database. Now I logged in with this number and my contacts list for suppose

  1. 9823484586
  2. +919988334856

Lets say the above 2 numbers are stored in database. But the 2nd contact , the user entered as 9988334856 without +91. Then finally in the listview instead of 2 numbers only 1 number is displaying as the second number is not matching with database number.

How can I solve this issue? Please help me in this regard.

If you are fetching all phones from DB, then you can use below code to match entered phone with phone from DB using PhoneNumberUtils.compare()

It compares phone numbers a and b, return true if they're identical enough for caller ID purposes.


private String getMatchedPhones(ArrayList<String> contactsFromDB, String phoneToMatch) {

    // Iterate all numbers and match 
    for (String numberFromDb : contactsFromDB) {
         if (PhoneNumberUtils.compare(numberFromDb, phoneToMatch)) {
             return phoneToMatch; // Or numberFromDb
         }
    }

    return null; // Or can custom msg. If not matched. 
}

I think best way to do it create your table with 4 column-

1)id 2)name(if needed) 3)country-code 4)phone number

And now on your UI prefix country code in a spiner and give phone-number type of field in a textview. And in your database use integer value to store number.

And from matching your phone number just pass this query- 1)phoneNumber = phoneNumberEditText.getText().toString(); 2) // Reading all contacts from database

    List<Contacts> number = db.getAllNumber();
                        for (final Contacts cn : number) {

                            if ((phoneNumber.equals(cn.getNumber()){
//do what you want
}
        }

Thanks!Hope it will help you.

Put these two lines in your XML file at that phone number edit text field

android:inputType="numberDecimal"
android:maxLength="10"

then he could not enter more than 10 numbers and only he should enter numbers. you can take country code in one more text field and validate with it.

For an Mobile number validation android provide InBuilt Patterns .But it only works in API level 8 and above.Try below one line code.

 /* method for phone number validation */
private Boolean Number_Validate(String number)
{              
    return  !TextUtils.isEmpty(number) && (number.length()==10) && android.util.Patterns.PHONE.matcher(number).matches();
}

you can call this mehtod by passing number in parameter,it return either true or false.

Number_Validate(number);

Hope you get your answer. Thanks.

Use TEXT not Int in your database. When you use Int, if the first number is 0 it will disregard so just use TEXT.

private boolean phvalidator(String ph2) {

    // TODO Auto-generated method stub
     String expression = "^[0-9-1+]{10,15}$";
       CharSequence inputStr = ph;
       Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(inputStr);
       return (matcher.matches())? true : false;

try this

final String mobile = emobile.getText().toString();
    if (!isValidMobile(mobile)){
        emobile.setError("Invalid Mobile");
    }

private boolean isValidMobile(String mobile) {
    if (!TextUtils.isEmpty(mobile)) {
        return Patterns.PHONE.matcher(mobile).matches();
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM