Simple Email Validation in Java

On January 31, 2020
8min read
Denys Velykozhon Marketing Lead @Railsware

Email validation is pivotal in ascertaining the quality and accuracy of email addresses. Keeping non-existent, inactive, or mistyped email accounts can unnecessarily fill your contact lists, leading to reduced deliverability and hurting your promotion efforts.

A valid email address is comprised of three sections: a local part, the at-sign (@), and a domain name. If this format is not followed, then the address becomes invalid.

According to the specifications of most Internet mail systems, the local part may use any of the following ASCII standard characters:

  • digits—0 to 9
  • lowercase and uppercase Latin letters—a to z and A to Z
  • printable characters—!#$%&’*+-/=?^_`{|}~
  • dot—., as long as it is not the initial or final character, or is not used consecutively

Furthermore, the domain name section of the email address may consist of the following characters:

  • digits—0 to 9
  • lowercase and uppercase Latin letters—a to z and A to Z
  • hyphen or dot — or . , as long as they are not the initial or final characters

Here are some examples of valid email addresses:

  • alice@example.com
  • alice.bob@example.com
  • alice@example.me.org

On the other hand, here are some examples of invalid email addresses:

  • alice.example.com (no @ character)
  • alice..bob@example.com (two consecutive dots not permitted)
  • alice@.example.com (domain cannot start with a dot)

Simple Email Validation in Java

The Apache Commons Validator package offers the building blocks for carrying out various data validation tasks. Specifically, its EmailValidator class allows you to verify email addresses easily.

To use it in your project, you can download it from here. You can also include it as a Maven dependency using the instructions available here.

Here is a code example that uses the Apache Commons Validator to perform simple email validation in Java:

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.validator.routines.EmailValidator;
public class EmailValidation {
   public static boolean isValidEmail(String email) {
       // create the EmailValidator instance
       EmailValidator validator = EmailValidator.getInstance();
       // check for valid email addresses using isValid method
       return validator.isValid(email);
   }
   public static void main(String[] args) {
       List<String> emails = new ArrayList<String>();
       // valid email addresses
       emails.add("alice@example.com");
       emails.add("alice.bob@example.com");
       emails.add("alice@example.me.org");
       //invalid email addresses
       emails.add("alice.example.com");
       emails.add("alice..bob@example.com");
       emails.add("alice@.example.com");
       for (String value : emails) {
           System.out.println("The Email address " + value + " is " + (isValidEmail(value) ? "valid" : "invalid"));
       }
   }
}

If we run the code, here is the output on the console:

The Email address alice@example.com is valid
The Email address alice.bob@example.com is valid
The Email address alice@example.me.org is valid
The Email address alice.example.com is invalid
The Email address alice..bob@example.com is invalid
The Email address alice@.example.com is invalid

As you can see on the above output, the package has correctly validated whether the provided email addresses are valid or not.

Email Regex in Java

If you want to have more control of the email validation process and verify a wide range of formats for email addresses, instead of the Apache Commons Validator package, you can use a regex string. 

A regular expression, commonly shortened to regex, allows you to create patterns for searching and matching strings of text. With regex, you can accurately check if the provided email addresses are in the required syntax.

For example, here is a simple regex that only checks for the ‘@’ in an email address—the other conditions for a valid email address will not be checked; and there can be any number of characters before and after the sign.

^(.+)@(.+)$

Let’s see how it can be implemented in a Java program:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^(.+)@(.+)$";
   public static void main(String args[]) {
       List<String> emails = new ArrayList<String>();
       // valid email addresses
       emails.add("alice@example.com");
       emails.add("alice.bob@example.co.in");
       emails.add("alice#@example.me.org");
       //invalid email addresses
       emails.add("alice.example.com");
       emails.add("alice#example.com");
       emails.add("@example.me.org");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("The Email address " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here’s the output on the console:

Email alice@example.com is valid
Email alice.bob@example.co.in is valid
Email alice@example.me.org is valid
Email alice.example.com is invalid
Email alice#example.com is invalid
Email @example.me.org is invalid

Notice that we used the java.util.regex.Pattern class to create a Pattern object, which is a compiled representation of the regex. Then, we used the java.util.regex.Matcher class to interpret the Pattern and carry out match operations against the string of email addresses.

Adding restrictions to local part and domain part

Now, let’s enhance the previous regular expression in Java for email to include some restrictions for the local part and the domain part.

Here are the restrictions we want to apply:

  • A-Z characters are permitted
  • a-z characters are permitted
  • 0-9 digits are permitted
  • Underscore(_), dash(-), and dot(.) are permitted
  • Other characters are not permitted

Here is the regular expression:

^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$

Let’s check if string is email Java using the above regex:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
   public static void main(String args[]) {
       //adding  emails to an array list
       List<String> emails = new ArrayList<String>();
       // valid email addresses
       emails.add("alice@example.com");
       emails.add("alice.bob@example.co.in");
       emails.add("alice1@example.me.org");
       emails.add("alice_bob@example.com");
       emails.add("alice-bob@example.com");
       //invalid email addresses
       emails.add("@example.com");
       emails.add("alice&example.com");
       emails.add("alice#@example.me.org");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("Email " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here is the output:

Email alice@example.com is valid
Email alice.bob@example.co.in is valid
Email alice1@example.me.org is valid
Email alice_bob@example.com is valid
Email alice-bob@example.com is valid
Email @example.com is invalid
Email alice&example.com is invalid
Email alice#@example.me.org is invalid

Checking for all valid characters in the local part

Let’s expand the previous regular expression to permit a wider variety of characters in the local part of an email address. Most of the characters included in this check are rarely used and some email systems may not handle them. Furthermore, some of these characters, such as the single quote (‘), can pose a security risk to your application, especially if you do not sanitize user inputs properly.

Here is the regular expression in Java for email validation:

^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$

Let’s use it to check for the validity of some email addresses:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
   public static void main(String args[]) {
       //adding  emails to an array list
       List<String> emails = new ArrayList<String>();
       //valid email addresses
       emails.add("alice@example.com");
       emails.add("alice.bob@example.co.in");
       emails.add("alice?bob@example.com");
       emails.add("alice`bob@example.com");
       emails.add("alice|bob@example.com");
       //invalid email addresses
       emails.add("@example.com");
       emails.add("aliceexample.com");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("Email " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here is the output:

Email alice@example.com is valid
Email alice.bob@example.co.in is valid
Email alice?bob@example.com is valid
Email alice`bob@example.com is valid
Email alice|bob@example.com is valid
Email @example.com is invalid
Email aliceexample.com is invalid

Try Mailtrap for Free

Checking for consecutive, trailing, or leading dots

Let’s continue improving our previous regular expressions to allow both the local part and the domain name part to have at least one dot. However, we’ll not allow any two consecutive dots or those dots appearing as the initial (or final) characters in the local part and domain part of the email address.

Here is the regular expression:

^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+↵\n" +
")*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$

Here is its implementation in a Java program:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+↵\n" +
           ")*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$";
   public static void main(String args[]) {
       //adding  emails to an array list
       List<String> emails = new ArrayList<String>();
       //valid email addresses
       emails.add("alice@example.com");
       emails.add("alice#bob@example.com");
       emails.add("alice`bob@example.com");
       //invalid email addresses
       emails.add("@example.com");
       emails.add("alice@example.com.");
       emails.add("alice@example..com");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("Email " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here is the output:

Email alice@example.com is valid
Email alice#bob@example.com is valid
Email alice`bob@example.com is valid
Email @example.com is invalid
Email alice@example.com. is invalid
Email alice@example..com is invalid

Adding restrictions to the domain name part

Finally, let’s improve the previous regex versions to ensure that the domain name part contains at least a single dot. Also, let’s add a condition that verifies that the section of the domain name after the final dot only comprises of letters. Furthermore, we’ll ensure that the top-level domain (such as .com) comprises of at least two to six letters.

Here is the regular expression:

^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$

Here is the Java program to check valid email address:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
   public static void main(String args[]) {
       //adding  emails to an array list
       List<String> emails = new ArrayList<String>();
       //valid email addresses
       emails.add("alice@example.com");
       emails.add("alice@example.co.in");
       emails.add("alice.bob@example.com");
       emails.add("alice_bob@example.com");
       emails.add("alice@example.company.in");
       //invalid email addresses
       emails.add(".alice@example.com");
       emails.add("alice@example.com.");
       emails.add("alice@example.c");
       emails.add("alice@example.company");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("Email " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here is the output:

Email alice@example.com is valid
Email alice@example.co.in is valid
Email alice.bob@example.com is valid
Email alice_bob@example.com is valid
Email alice@example.company.in is valid
Email .alice@example.com is invalid
Email alice@example.com. is invalid
Email alice@example.c is invalid
Email alice@example.company is invalid

Using OWASP validation regex

Besides creating your own regular expression in Java for email verification, you can also use the ones that are freely provided by the OWASP organization

Here is an example of an OWASP validation regex:

^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$

Let’s use it to check if string is email Java:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailValidation {
   private static final String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
   public static void main(String args[]) {
       //adding  emails to an array list
       List<String> emails = new ArrayList<String>();
       //valid email addresses
       emails.add("alice@example.com");
       emails.add("alice.bob@example.com");
       emails.add("alice@example.me.org");
       //invalid email addresses
       emails.add("alice.example.com");
       emails.add("alice..bob@example.com");
       emails.add("alice@.example.me.org");
       //initialize the Pattern object
       Pattern pattern = Pattern.compile(regex);
       //searching for occurrences of regex
       for (String value : emails) {
           Matcher matcher = pattern.matcher(value);
           System.out.println("Email " + value + " is " + (matcher.matches() ? "valid" : "invalid"));
       }
   }
}

Here is the output:

Email alice@example.com is valid
Email alice.bob@example.com is valid
Email alice@example.me.org is valid
Email alice.example.com is invalid
Email alice..bob@example.com is invalid
Email alice@.example.me.org is invalid

Conclusion

That’s how to create a Java program to check valid email address using regular expressions. 

As illustrated by the examples above, there is no one-size-fits-all solution for validating email addresses using regex. Therefore, instead of trying to come up with a perfect solution that fits all cases, go for a simple or relaxed regular expression that best meets your needs. Although regex is a powerful email validation technique, it cannot guarantee a panacea. 

Ultimately, the best way of ensuring that an email address is valid is to send email using Java with a link or code for the recipient to undertake another authentication step. That way, you may not need to use complicated regular expressions that may eliminate valid email addresses.

Happy sending emails!

Article by Denys Velykozhon Marketing Lead @Railsware

With a decade of experience in content marketing, I specialize in launching custom email campaigns that drive engagement and conversions. Let’s explore the art and science of creating personalized content that resonates with your audience and delivers outstanding results. Join me on this blog to master the art of impactful email campaigns.