Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android में EditText पर ईमेल पता मान्य कैसे करें?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में EditText पर ईमेल पते को कैसे मान्य किया जाए।

चरण 1 - नया प्रोजेक्ट बनाएं

Android Studio में: File → New Project चुनें और आवश्यक विवरण भरें।

चरण 2 - activity_main.xml लेआउट जोड़ें

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:hint="ईमेल आईडी दर्ज करें" />
    <Button
        android:id="@+id/btnCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerInParent="true"
        android:text="मान्यता जांचें"
        android:textAlignment="center"
        android:textColor="#000"
        android:textSize="18sp" />
</RelativeLayout>

चरण 3 - MainActivity.kt में मान्यता लॉजिक जोड़ें

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var btnCheck: Button
    private lateinit var editText: EditText
    private val emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        btnCheck = findViewById(R.id.btnCheck)
        editText = findViewById(R.id.editText)

        btnCheck.setOnClickListener {
            val input = editText.text.toString().trim()
            if (input.isEmpty()) {
                Toast.makeText(this, "कृपया ईमेल पता दर्ज करें", Toast.LENGTH_SHORT).show()
            } else if (input.matches(emailPattern.toRegex())) {
                Toast.makeText(this, "मान्य ईमेल पता", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "अमान्य ईमेल पता", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

चरण 4 - AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.emailvalidation">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ऐप चलाएं

डिवाइस कनेक्ट करें, Run पर क्लिक करें। ईमेल दर्ज करें और "मान्यता जांचें" बटन दबाएं। सही फॉर्मेट (उदाहरण: user@example.com) के लिए "मान्य ईमेल पता" दिखेगा, अन्यथा "अमान्य ईमेल पता"।

  1. Android में EditText इनपुट को कैसे वैलिडेट करें? Android में EditText इनपुट को कैसे वैलिडेट करें?

    Android में EditText इनपुट वैलिडेट करना यह उदाहरण दर्शाता है कि Android में EditText इनपुट को कैसे वैलिडेट किया जाता है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/activity_main.xml में कोड जोड़े

  1. एंड्रॉइड में ईमेल एड्रेस कैसे वैलिडेट करें एंड्रॉइड में ईमेल एड्रेस कैसे वैलिडेट करें

    रैगुलर एक्सप्रेशन (Regex) और Patterns API का उपयोग करके ईमेल वैलिडेशन एंड्रॉइड में ईमेल एड्रेस वैलिडेट करने के लिए java.util.regex.Pattern और Matcher क्लासेज या android.util.Patterns हेल्पर क्लास का उपयोग किया जा सकता है। यह उदाहरण दोनों दृष्टिकोण दिखाता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studi

  1. - C# में ईमेल एड्रेस कैसे वैलिडेट करें?

    C# में ईमेल एड्रेस को वैलिडेट करने के कई तरीके हैं। 1. System.Net.Mail का उपयोग करना System.Net.Mail नेमस्पेस में सिंपल मेल ट्रांसफर प्रोटोकॉल (SMTP) सर्वर को डिलीवरी के लिए इलेक्ट्रॉनिक मेल भेजने के लिए उपयोग की जाने वाली क्लासेज होती हैं। 2. System.Text.RegularExpressions का उपयोग करना एक अपरिवर