บทนำ
ในหน้าล็อกอินหรือสมัครสมาชิก มักต้องรับอีเมลจากผู้ใช้ ต้องตรวจสอบว่าข้อความที่กรอกเป็นรูปแบบอีเมลที่ถูกต้องหรือไม่ ตัวอย่างนี้ใช้ Regular Expression (Regex) เพื่อตรวจสอบรูปแบบอีเมล
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:id="@+id/email"
android:hint="Email id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/valid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check validation"
android:layout_marginTop="16dp"/>
</LinearLayout>
เพิ่ม android:inputType="textEmailAddress" ให้แป้นพิมพ์แสดง @ และ . อัตโนมัติ
ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
// Regex สำหรับตรวจสอบรูปแบบอีเมลพื้นฐาน
private static final String EMAIL_REGEX = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText email = findViewById(R.id.email);
Button valid = findViewById(R.id.valid);
valid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = email.getText().toString().trim();
if (input.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter email id", Toast.LENGTH_LONG).show();
return;
}
Pattern pattern = Pattern.compile(EMAIL_REGEX);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
Toast.makeText(MainActivity.this, "Email id is valid", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Not a valid email id", Toast.LENGTH_LONG).show();
}
}
});
}
}
การทดสอบแอปพลิเคชัน
รันแอปบนอุปกรณ์จริง
กรณีที่ 1: ไม่กรอกอะไรกดปุ่ม

แสดง Toast: "Please enter email id"
กรณีที่ 2: กรอกรูปแบบผิด

แสดง Toast: "Not a valid email id"
กรณีที่ 3: กรอกรูปแบบถูกต้อง

แสดง Toast: "Email id is valid"
คำอธิบาย Regex Pattern
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$
| ส่วน | ความหมาย |
|---|---|
^ |
เริ่มต้นข้อความ |
[A-Za-z0-9._%+-]+ |
ตัวอักษร อนุสาวรณีย์ที่อนุญาต สำหรับ Local part (ก่อน @) อย่างน้อย 1 ตัว |
@ |
ตัว @ จริงๆ |
[A-Za-z0-9.-]+ |
Domain name (หลัง @) |
\\. |
จุด (.) จริงๆ |
[A-Za-z]{2,} |
Top-level domain (com, net, org, th, co.th ฯลฯ) อย่างน้อย 2 ตัวอักษร |
$ |
สิ้นสุดข้อความ |
วิธีที่ทันสมัยกว่า (API 8+)
Android มีคลาส android.util.Patterns พร้อม Pattern สำหรับอีเมลในตัว:
import android.util.Patterns;
// ...
if (Patterns.EMAIL_ADDRESS.matcher(input).matches()) {
// valid email
}
สรุป
ใช้ Regex หรือ Patterns.EMAIL_ADDRESS เพื่อตรวจสอบรูปแบบอีเมลใน EditText ช่วยป้องกันข้อมูลผิดพลาดก่อนส่งไปเซิร์ฟเวอร์