Validate Email Address with Regular Expression using Kotlin

Introduction

This tutorial will show you how to validate email address with regular expression using Kotlin. In this post, email validation in Kotlin will be done using regular expression or regex pattern.

In web applications or standalone applications where we need to send emails to intended recipients, it is recommended to check the valid email address before sending email to those particular emails for safety purpose.

Here we will use regular expression (regex) to validate email address using Kotlin programming language. We will also see how to create Kotlin project in Eclipse.

Prerequisites

Eclipse 4.12, Gradle 5.6, Kotlin 1.3.50, Java 12 (or 1.8)

Read before you proceed below how to create Kotlin project in Eclipse

Build Script

Update the default generated build script to include the dependency as shown below:

buildscript {
	ext {
		kotlin_version = '1.3.50'
	}
	repositories {
		mavenLocal()
		mavenCentral()
	}
	dependencies {
		classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
	}
}
apply plugin: "kotlin"
repositories {
	mavenLocal()
	mavenCentral()
}
dependencies {
	implementation "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
}

Final Results

We will test our program using some sample data to validate email address with regular expression using Kotlin.

For each of the test data, the program prints either true or false according to whether valid or invalid email address.

Sample Input

Julia.abc@
Samantha@com
Samantha_21.
.1Samantha
Samantha@10_2A
JuliaZ007
Julia@007.com
_Julia007.com
_Julia007@abc.co.in
Julia.007@abc.com

Sample Output

false
false
false
false
false
false
true
false
false
true

Creating Kotlin Class

The source code is given below. We have used here regular expression for validating email address. This email validation regular expression is not a rigid one. So if you need more rigid version of regular expression for email address validation, then you can check the tutorial under EmplyeeEmailValidation rule.

package com.roytuts.kotlin.validate.email.regex
class EmailValidator {
	companion object {
		@JvmStatic
		val EMAIL_REGEX = "^[A-Za-z](.*)([@]{1})(.{1,})(\\.)(.{1,})";
		fun isEmailValid(email: String): Boolean {
			return EMAIL_REGEX.toRegex().matches(email);
		}
	}
}
fun main(args: Array<String>) {
	println(EmailValidator.isEmailValid("Julia.abc@"));
	println(EmailValidator.isEmailValid("Samantha@com"));
	println(EmailValidator.isEmailValid("Samantha_21."));
	println(EmailValidator.isEmailValid(".1Samantha"));
	println(EmailValidator.isEmailValid("Samantha@10_2A"));
	println(EmailValidator.isEmailValid("JuliaZ007"));
	println(EmailValidator.isEmailValid("Julia@007.com"));
	println(EmailValidator.isEmailValid("_Julia007.com"));
	println(EmailValidator.isEmailValid("_Julia007@abc.co.in"));
	println(EmailValidator.isEmailValid("Julia.007@abc.com"));
}

Running the Application

Do right click on the above Kotlin class and select Run As -> Kotlin Application.

You will see the expected output as shown in the Final Results -> Sample Output section.

If you get class not found exception, you may try to do the following:

Do right click on the project -> Configure build path -> Check the correct version of JDK is selected.

Another option is to do right click on the project -> Kotlin compiler -> JVM target version (select JDK version) and JDK Home (select your Java installation home directory).

Source Code

You can download source code.

Thanks for reading.

2 thoughts on “Validate Email Address with Regular Expression using Kotlin

Leave a Reply

Your email address will not be published. Required fields are marked *