Java Email Validation

In designing forms, email plays an important role. The email can be of our username or login id. An email has its own structure, and before using it, we need to validate it. In Java, email validation is performed by using the regular expression.

Email validation is required in any of the application that looks for email addresses as required information at the registration stage.

There are five ways through which we can perform email validation using a regular expression.

  1. Simplest regex to validate email.
  2. Adding restriction on user name part.
  3. E-mail validation permitted by RFC 5322.
  4. Regex to restrict leading, trailing, or consecutive dots in emails
  5. Regex to restrict no. of characters in the top-level domain.
Java Email Validation

Simplest regex to validate an email

The regular expression ^(.+)@(.+)$ is the simplest regular expression the checks the @ symbol only. It doesn't care about the characters before and after the '@' symbol. Let's take an example to understand how it validates the email address.

EmailValidation1.java

Output:

Java Email Validation

Adding restriction on the User Name part

The regular expression "^[A-Za-z0-9+_.-]+@(.+)$" also check the user name part of the email address. In order to check the user name part of the email, we have added some restrictions by using a regular expression. The regex "^[A-Za-z0-9+_.-]+@(.+)$", ^[A-Za-z0-9+_.-] defines the following restriction.

  1. A-Z characters are allowed
  2. a-z characters are allowed
  3. 0-9 numbers are allowed
  4. Additionally email can contain dot(.), underscore(_), and dash(-).
  5. The remaining characters are not allowed.

Let's take an example to understand how it validates the email address.

EmailValidation2.java

Output:

Java Email Validation

Email Validation Permitted by RFC 5322

To validate the email permitted by RFC 5322, we use "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$" regular expression. It uses all the characters which are allowed by RFC for the email message format. In these characters, some characters present a risk when they pass directly from user input to an SQL statement. These characters are basically pipe character(|), single quote(''), and etc.

Let's take an example to understand how it validates the email address.

EmailValidation3.java

Output:

Java Email Validation

Restrict E-mail from Trailing, Consecutive, and Leading

The regular expression "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" restrict us to add consecutive dots, trailing, and leading. An email can contain more than one dot in both the local part and the domain name, but consecutive dots are allowed. Our email can also not be started or ended with a dot. The regex validates the email based on these three conditions too.

Let's take an example to understand how it validates the email address.

EmailValidation4.java

Output:

Java Email Validation

Restrict email to enter a number of characters in the top-level domain

The regular expression "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$" check for at least one dot in the domain name and after the dot, it consist only the letters. The top-level domain should have only two to six letters which is also checked by this regex.

Let's take an example to understand how it validates the email address.

EmailValidation5.java

Output:

Java Email Validation

All the above discussed regular expressions are used for email validation. The last regular expression strictly validates the email with more conditions and rules. In Java, we use the last discussed regular expression mostly to validate the email.






Latest Courses