- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to get phone number in android?
- How to obtain the phone number of the Android phone programmatically using Kotlin?
- How to get default phone number in android?
- How to verify action bar is showing or not in android?
- How to obtain the phone number of the android phone programmatically?
- How to verify action bar tittle is truncating or not in android?
- How to get phone number from content provider in android?
- How to detect device is Android phone or Android tablet?
- How do I get the dialer to open with the phone number displayed in Android using Kotlin?
- How to check whether a number is prime or not using Python?
- How to detect if a user is using an Android tablet or a phone using Kotlin?
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
