Join us

Simple Email Validation in Java

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:

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

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:

Here’s the output on the console:

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:

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

Here is the output:

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:

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

Here is the output:

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:

Here is its implementation in a Java program:

Here is the output:

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:

Here is the Java program to check valid email address:

Here is the output:

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:

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

Here is the output:

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!

That's it! Thank you for reading our guide on email validation in Java with regular expression that was originally published on Mailtrap Blog.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

User Popularity
693

Influence

68k

Total Hits

49

Posts