Java Regex Email Validation Example

In this post, we will talk about validation email using regex in Java. We will talk about different java regex email validation example.

 

Java Regex Email Validation

Email validation using regex is a common task and many validation framework provides this feature. You may need to validate the email address before using it to make sure it’s a valid email. In this article, we will look at the how to validate email in Java using regex. Please keep in mind that it’s the validation to ensure email conforms to the standard convention. It can not identify if given email is active and valid (e.g. valid domain etc). Before we get in more details, let’s see what parts make an email.

  1. localpart.
  2. @ sign
  3. domain

In our case, [email protected]

  1. contact-us – is the local part of the email.
  2. @ – shows the end of local part and starting of domain.
  3. javadevjournal.com – It’s the domain name.

We will cover the following aspects of the email validation:

  1. Simple email validation – An an example, not useful for production application.
  2. Strict email validation.
  3. Validation which includes HTML5 email validation specifications.

Let’s get in to the code

 

1. Simple Java Regex Email Validation

We will start with a simple regular expression to validate the email. This regex for email validation will only check for the @ symbol and make sure there is character before and after the @ symbol. Here is how the regex look like:

^(.+)@(.+)$

Let’s see an example:

package com.javadevjournal.regex.email;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class contains different utility methods to validate emails
 */
public class SimpleEmailValidation implements EmailValidation{

    private static  Pattern EMAIL_REGEX= Pattern.compile("^(.+)@(.+)$");
    

    /**
     * A simple validation which check for @ symbol and make sure there is
     * some character before and after the @ symbol
     * @param email
     * @return
     */
    @Override
    public boolean validateEmailAddress(String email) {
        final Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }

}

Here is our Unit test for this regular expression

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SimpleSimpleEmailValidationTest {

    private static final Logger LOG = LogManager.getLogger(SimpleSimpleEmailValidationTest.class);

    @DisplayName("Validate email using simple regex")
    @ParameterizedTest
    @ValueSource(strings = {"[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
     //invalid emails
     "contact.us#javadevjournal.com",
     "@javadevjournal.com",
     "[email protected]"
    })
    void validate_email_simpleRegex(String email){

        SimpleEmailValidation validation = new SimpleEmailValidation();
        assertTrue(validation.validateEmailAddress(email));
    }
}

when we run our application, here is the output:

Java Regex Email Validation

 

2. Java Regex Email Validation With Domain Name length Validation (Recommended)

This is the recommended approach to validate email in Java using regex. This regex will going to meet 99% of your requirements and can be tweaked easily to add additional details.Most of the email validation regex fails when it comes to check the length of the domain name (e.g. .com, .org, .media).

These regular expression takes in to account only some high level and most used domain and will ignore domain with longer name (e.g. .corporate). They sometime also ignore the fact that domain name can be also contains second level (e.g javadevjournal.media.org) . Let’s see how this regular expression looks like:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidationWithDomain implements EmailValidation {

    private static Pattern EMAIL_REGEX= Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

    /**
     * This email validation use the regular expression to check the email pattern.
     * It also check the length of the domain name (e.g. .com , .net, .media etc). You can always modify
     * the regular expression to increase the length which checking the domain name character length
     * @param email
     * @return
     */
    @Override
    public boolean validateEmailAddress(String email) {
        final Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }
}

Here is the Unit test case for this

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SimpleEmailValidationWithDomainTest {

    @ParameterizedTest(name = "#{index} - Test with email = {0}")
    @MethodSource("validEmailList")
    void validate_email_test(String email) {

        EmailValidation validation = new EmailValidationWithDomain();
        assertTrue(validation.validateEmailAddress(email));
    }

    @ParameterizedTest(name = "#{index} - Test with email = {0}")
    @MethodSource("invalidEmailList")
    void invalid_emails_test(String email) {

        EmailValidation validation = new EmailValidationWithDomain();
        assertFalse(validation.validateEmailAddress(email));
    }

    static List < String > validEmailList() {
        return Arrays.asList(
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]"
        );
    }

    static List < String > invalidEmailList() {
        return Arrays.asList(
            "contact.us#javadevjournal.com",
            "@javadevjournal.com"
        );
    }
}

When we run the test cases, here is the output:

Java email regex examples

There are few important point:

  1. #5 failed in out test case because we set the maximum length of the domain name as 6 in our regular expression (.[A-Z]{2,6}$), you can change it as per your requirement. Here are the updated results with updated regular expression (.[A-Z]{2,12}$)

Java email validation using regex

 

3. Java Email Validation (RFC 5322)

RFC 5322 governs the email message format. You can also adapt your email validation regex as per RFC 5322 format. Here is the regex:

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

There are few things which you should be aware of , if you like to use RFC 5322 java regex email validation.

  1. It permits certain characters which can cause security risks.
  2. It permits | and single quotes ('), which can cause some issue.
  3. If you like to use this, please make sure to sanitize/ escape characters before storing it.
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RFC5322EmailValidation implements EmailValidation {

    private static Pattern EMAIL_REGEX= Pattern.compile("^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$", Pattern.CASE_INSENSITIVE);

    /**
     * Java regular expression to validate email using the RFC 5322. Keep in mind that it also allows
     * few characters which can pause security risks (e.g. | character)
     * @param email
     * @return boolean
     */
    @Override
    public boolean validateEmailAddress(String email) {
        final Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }
}

Here is the Junit test case for this.

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RFC5322SimpleEmailValidationTest {

    @ParameterizedTest(name = "#{index} - Run test with email = {0}")
    @MethodSource("validEmailList")
    void valid_email_test(String email) {

        EmailValidation validation = new RFC5322EmailValidation();
        assertTrue(validation.validateEmailAddress(email));
    }

    @ParameterizedTest(name = "#{index} - Run test with email = {0}")
    @MethodSource("invalidEmailList")
    void invalid_emails_test(String email) {
        EmailValidation validation = new RFC5322EmailValidation();
        assertFalse(validation.validateEmailAddress(email));
    }

    static List < String > validEmailList() {
        return Arrays.asList(
            "[email protected]",
            "[email protected]",
            "[email protected]",
            "[email protected]"
        );
    }

    static List < String > invalidEmailList() {
        return Arrays.asList(
            "@javadevjournal.com",
            "[email protected]"
        );
    }
}

Here is the output

java regex email validation

 

4. Regex Email Validation with leading, trailing and consecutive dots

<p? Here is the Regex.

^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\\\.[a-zA-Z0-9-]+)*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidationWithDoubleDots implements EmailValidation {

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

    /**
     * Email validation regex to restrict leading, trailing dots. It also will check if the
     * email contains consecutive dots.
     * @param email
     * @return
     */
    @Override
    public boolean validateEmailAddress(String email) {
        final Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }
}

Unit test case:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SimpleEmailValidationConsecutiveDotsTest {

    @ParameterizedTest
    @MethodSource("validEmailList")
    void valid_email_test(String email) {

        EmailValidation validation = new EmailValidationWithDoubleDots();
        assertTrue(validation.validateEmailAddress(email));
    }

    @ParameterizedTest
    @MethodSource("invalidEmailList")
    void invalid_emails_test(String email) {
        EmailValidation validation = new EmailValidationWithDoubleDots();
        assertFalse(validation.validateEmailAddress(email));
    }

    static List < String > validEmailList() {
        return Arrays.asList(
            "[email protected]",
            "[email protected]",
            "[email protected]"

        );
    }

    static List < String > invalidEmailList() {
        return Arrays.asList(
            "[email protected]",
            "[email protected].",
            "[email protected]"
        );
    }
}

 

5. W3C HTML5 Email validation (Recommended)

The W3C HTML5 specification provides a field with email and it also validate the email. Internally they use the regex to validate if user entered a valid email or not. Here is the Regex used for the email validation.

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

This regex violate some of the specification under RFC 5322, but it’s more practical and cater to the real world use cases.If you want to align yourself to the HTML specification, you should go by this regex to validate your emails.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class W3CHTML5EmailValidation implements EmailValidation {

    private static Pattern EMAIL_REGEX= Pattern.compile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");

    @Override
    public boolean validateEmailAddress(String email) {
        final Matcher matcher = EMAIL_REGEX.matcher(email);
        return matcher.matches();
    }
}

 

Summary

In this article, we saw the different regex to validate the email address in Java. Regex is a powerful tool and can be handy. We started from basic example and moved to the more real world regex for the email validations. The source code for this article is available on our GitHub repository.