Phone number validation Android

Viewed 135043

How do I check if a phone number is valid or not? It is up to length 13 (including character + in front).

How do I do that?

I tried this:

String regexStr = "^[0-9]$";

String number=entered_number.getText().toString();  

if(entered_number.getText().toString().length()<10 || number.length()>13 || number.matches(regexStr)==false  ) {
    Toast.makeText(MyDialog.this,"Please enter "+"\n"+" valid phone number",Toast.LENGTH_SHORT).show();
    // am_checked=0;
}`

And I also tried this:

public boolean isValidPhoneNumber(String number)
{
     for (char c : number.toCharArray())
     {
         if (!VALID_CHARS.contains(c))
         {
            return false;
         }
     }
     // All characters were valid
     return true;
}

Both are not working.

Input type: + sign to be accepted and from 0-9 numbers and length b/w 10-13 and should not accept other characters

16 Answers

We can use pattern to validate it.

android.util.Patterns.PHONE

public class GeneralUtils {

    private static boolean isValidPhoneNumber(String phoneNumber) {
        return !TextUtils.isEmpty(phoneNumber) && android.util.Patterns.PHONE.matcher(phoneNumber).matches();
    }

}
 String validNumber = "^[+]?[0-9]{8,15}$";

            if (number.matches(validNumber)) {
                Uri call = Uri.parse("tel:" + number);
                Intent intent = new Intent(Intent.ACTION_DIAL, call);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
                return;
            } else {
                Toast.makeText(EditorActivity.this, "no phone number available", Toast.LENGTH_SHORT).show();
            }
val UserMobile = findViewById<edittext>(R.id.UserMobile)
val msgUserMobile: String = UserMobile.text.toString()
fun String.isMobileValid(): Boolean {
    // 11 digit number start with 011 or 010 or 015 or 012
    // then [0-9]{8} any numbers from 0 to 9 with length 8 numbers
    if(Pattern.matches("(011|012|010|015)[0-9]{8}", msgUserMobile)) {
        return true
    }
    return false
}

if(msgUserMobile.trim().length==11&& msgUserMobile.isMobileValid())
    {//pass}
else 
    {//not valid} 
^\+201[0|1|2|5][0-9]{8}

this regex matches Egyptian mobile numbers

Here is how you can do it succinctly in Kotlin:

fun String.isPhoneNumber() =
            length in 4..10 && all { it.isDigit() }

What about this method:

private static boolean validatePhoneNumber(String phoneNumber) {
    // validate phone numbers of format "1234567890"
    if (phoneNumber.matches("\\d{10}"))
        return true;
        // validating phone number with -, . or spaces
    else if (phoneNumber.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}"))
        return true;
        // validating phone number with extension length from 3 to 5
    else if (phoneNumber.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}"))
        return true;
        // validating phone number where area code is in braces ()
    else if (phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}"))
        return true;
        // Validation for India numbers
    else if (phoneNumber.matches("\\d{4}[-\\.\\s]\\d{3}[-\\.\\s]\\d{3}"))
        return true;
    else if (phoneNumber.matches("\\(\\d{5}\\)-\\d{3}-\\d{3}"))
        return true;

    else if (phoneNumber.matches("\\(\\d{4}\\)-\\d{3}-\\d{3}"))
        return true;
        // return false if nothing matches the input
    else
        return false;
}

  System.out.println("Validation for 1234567890 : " + validatePhoneNumber("1234567890"));
  System.out.println("Validation for 1234 567 890 : " + validatePhoneNumber("1234 567 890")); 
  System.out.println("Validation for 123 456 7890 : " + validatePhoneNumber("123 456 7890"));
  System.out.println("Validation for 123-567-8905 : " + validatePhoneNumber("123-567-8905"));
  System.out.println("Validation for 9866767545 : " + validatePhoneNumber("9866767545"));
  System.out.println("Validation for 123-456-7890 ext9876 : " + validatePhoneNumber("123-456-7890 ext9876"));

And the outputs:

Validation for 1234567890 : true
Validation for 1234 567 890 : true
Validation for 123 456 7890 : true
Validation for 123-567-8905 : true
Validation for 9866767545 : true
Validation for 123-456-7890 ext9876 : true

For more info please refer to this link.

Try this function it should work.

fun isValidPhone(phone: String): Boolean =
    phone.trimmedLength() in (10..13) && Patterns.PHONE.matcher(phone).matches()
Related