Join us
@sofiatarhonska ・ Oct 18,2022 ・ 5 min read ・ 2106 views ・ Originally posted on mailtrap.io
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:
Furthermore, the domain name section of the email address may consist of the following characters:
Here are some examples of valid email addresses:
On the other hand, here are some examples of invalid email addresses:
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.
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.
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:
Here is the regular expression:
Let’s check if string is email Java using the above regex:
Here is the output:
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:
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:
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:
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:
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.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.