Skip to content

Validating a Phone Number with android.util.patterns

Android, Phone Number Validation1 min read

The process of validating user input is an important aspect of mobile app development. When it comes to validating phone numbers in an Android application, the android.util.Patterns class provides a convenient way to perform this task. In this article, we will explore how to use android.util.Patterns to validate phone numbers in your Android app, along with some practical examples.

What is android.util.Patterns?

The android.util.Patterns class is a utility class provided by the Android SDK that contains predefined regular expressions for common patterns such as email addresses, phone numbers, and URLs. It offers a set of patterns and methods to match and validate different types of data.

Validating a Phone Number

To validate a phone number using android.util.Patterns, we can make use of the Patterns.PHONE constant. Here's an example of how to validate a phone number in Kotlin:

1import android.util.Patterns
3fun isValidPhoneNumber(phoneNumber: String): Boolean {
4 return Patterns.PHONE.matcher(phoneNumber).matches()

In the above example, we define a function isValidPhoneNumber() that takes a phoneNumber parameter. We use the Patterns.PHONE constant to obtain the pattern for validating phone numbers and then apply the matches() method of the Matcher class to check if the given phoneNumber matches the pattern.

Let's see how we can use this function in practice.

Example 1: Basic Validation

1val phoneNumber = "+1234567890"
2val isValid = isValidPhoneNumber(phoneNumber)
3if (isValid) {
4 // Phone number is valid
5} else {
6 // Phone number is invalid

In this example, we pass a phone number string ("+1234567890") to the isValidPhoneNumber() function and check the returned value. If the phone number is valid, you can perform further actions or display a success message. Otherwise, you can handle the case where the phone number is invalid.

Example 2: Input Validation in an EditText

To provide real-time validation for a phone number input field, we can leverage the android.text.TextWatcher interface to listen for changes in the input text and update the validation status accordingly.

1import android.text.Editable
2import android.text.TextWatcher
3import android.widget.EditText
5fun setupPhoneNumberValidation(editText: EditText) {
6 editText.addTextChangedListener(object : TextWatcher {
7 override fun afterTextChanged(s: Editable) {}
9 override fun beforeTextChanged(
10 s: CharSequence,
11 start: Int,
12 count: Int,
13 after: Int
14 ) {
15 }
17 override fun onTextChanged(
18 s: CharSequence,
19 start: Int,
20 before: Int,
21 count: Int
22 ) {
23 val isValid = isValidPhoneNumber(s.toString())
24 editText.error = if (isValid) null else "Invalid phone number"
25 }
26 })

In this example, the setupPhoneNumberValidation() function sets up a TextWatcher for the given EditText to perform real-time validation. Whenever the user enters or modifies the text, the onTextChanged() method is called, which invokes the isValidPhoneNumber() function to validate the entered phone number. The error message is set on the EditText if the phone number is invalid.

Validating user input is crucial for ensuring data integrity and providing a seamless user experience. With the help of android.util.Patterns, we can easily validate phone numbers in our Android applications. By incorporating input validation techniques like the ones discussed in this article, you can enhance the reliability and usability of your app.