How to Validate an Email Using Kotlin in Android

In this article, we will learn how to Validate an Email Address using Kotlin in Android. Firstly, we have to create a function that will check and validate the email addresses.

We can do email validation in two different ways. By using a regular expression or Regex and by using the Android utility class.

Validate an Email in Kotlin

Example of Regular Expression

We can use a regular expression to validate a string and we can use a regex pattern to check if a string is a valid email or not. The code is given below as follows.

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import java.util.regex.Pattern

class MainActivity : AppCompatActivity() {
    val EMAIL_ADDRESS_PATTERN = Pattern.compile(
        "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                "\\@" +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
                "(" +
                "\\." +
                "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
                ")+"
    )
    fun isValidString(str: String): Boolean{
        return EMAIL_ADDRESS_PATTERN.matcher(str).matches()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val emails = arrayOf<String>("hello@gmail.com", "one.com", "")

        emails.forEach {
            Log.d("MainActivity", "is valid email $it => ${isValidString(it)}")
        }
    }
}

The Code reference is stack overflow.

If you check the logcat output then you will see something like shown as below.

com.codevscolor.myapplication D/MainActivity: is valid email hello@gmail.com => true
com.codevscolor.myapplication D/MainActivity: is valid email one.com => false
com.codevscolor.myapplication D/MainActivity: is valid email  => false

Alternatively:

Validate Email Address using android.util.Patterns

As we know that android.util.Patterns provide different types of pattern matches. Below is the complete program that uses android.util.Patterns

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity : AppCompatActivity() {

    fun isValidString(str: String): Boolean{
        return android.util.Patterns.EMAIL_ADDRESS.matcher(str).matches()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val emails = arrayOf<String>("hello@gmail.com", "one.com")

        emails.forEach {
            Log.d("MainActivity","is valid email $it => ${isValidString(it)}")
        }
    }
}

We can also configure these functions as Extension function.

That’s it. This is how to Validate an Email Using Kotlin in Android Studio

If you have any questions or suggestions, feel free to ask in the comments section below.

Next Article

Regex in Kotlin Android Studio

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: