How to verify enter number is phone number or not using regex in android?


This example demonstrates how to verify enter number is phone number or not, using regex in android.

Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 - Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/enterNumber"
      android:layout_width = "match_parent"
      android:hint = "Enter phone number"
      android:layout_height = "wrap_content" />
   <TextView
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:textAlignment = "center"
      android:text = "click"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
</LinearLayout>

In the above code, we have taken Edit text and text view. when the user clicks on text view, it will take data from edit text and valid the data.

Step 3 - Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView text;
   EditText enterNumber;
   String pattern = "^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$";
   Matcher m;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      text = findViewById(R.id.text);
      enterNumber = findViewById(R.id.enterNumber);
      text.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Pattern r = Pattern.compile(pattern);
            if (!enterNumber.getText().toString().isEmpty()) {
               m = r.matcher(enterNumber.getText().toString().trim());
            } else {
               Toast.makeText(MainActivity.this, "Please enter mobile number ", Toast.LENGTH_LONG).show();
            }
           if (m.find()) {
              Toast.makeText(MainActivity.this, "MATCH", Toast.LENGTH_LONG).show();
           } else {
              Toast.makeText(MainActivity.this, "NO MATCH", Toast.LENGTH_LONG).show();
           }
        }
     });
}
}

In the above code, we have taken regex expression to valid edit text data. use the following code to valid edit text data.

String pattern = "^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$";
Matcher m;
.....
Pattern r = Pattern.compile(pattern);
if (!enterNumber.getText().toString().isEmpty()) {
   m = r.matcher(enterNumber.getText().toString().trim());
} else {
   Toast.makeText(MainActivity.this, "Please enter mobile number ", Toast.LENGTH_LONG).show();
}
if (m.find()) {
   Toast.makeText(MainActivity.this, "MATCH", Toast.LENGTH_LONG).show();
} else {
   Toast.makeText(MainActivity.this, "NO MATCH", Toast.LENGTH_LONG).show();
}
.........

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen -

In the above code, we have given the correct phone number format. it gives proper validation result as "Match". Now we are giving unknown format in edit text. it going the result as "Not match" as shown below -

Click here to download the project code

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements