วิธีตรวจสอบเบอร์โทรศัพท์ด้วย Regex ใน Android
ตัวอย่างนี้สาธิตวิธีตรวจสอบว่าเบอร์ที่ป้อนเป็นเบอร์โทรศัพท์หรือไม่ โดยใช้ Regex ใน Android
ขั้นตอนที่ 1 - สร้างโปรเจกต์ใหม่ใน Android Studio
ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2 - เพิ่มโค้ดใน res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://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>
ในโค้ดด้านบน เราใช้ EditText และ TextView เมื่อผู้ใช้คลิกที่ TextView จะดึงข้อมูลจาก EditText และตรวจสอบข้อมูล
ขั้นตอนที่ 3 - เพิ่มโค้ดใน 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();
}
}
});
}
}
ในโค้ดด้านบน เราใช้ Regex Expression เพื่อตรวจสอบข้อมูล EditText ใช้โค้ดต่อไปนี้เพื่อตรวจสอบข้อมูล EditText:
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();
}
.........
ลองรันแอปพลิเคชันของคุณ สมมติว่าคุณเชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์แล้ว เพื่อรันแอปจาก Android Studio ให้เปิดไฟล์กิจกรรมของโปรเจกต์หนึ่งและคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก และตรวจสอบอุปกรณ์ซึ่งจะแสดงหน้าจอเริ่มต้น

ในโค้ดด้านบน เราให้รูปแบบเบอร์โทรศัพท์ที่ถูกต้อง ให้ผลการตรวจสอบว่า "Match" ตอนนี้เราให้รูปแบบที่ไม่รู้จักใน EditText จะได้ผลลัพธ์ "Not Match" ดังภาพด้านล่าง
