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: