Java Email Validation using Regex

Email validation using regular expressions is a common task that may be required in any application accepting email addresses as required information in the registration step. There may be more usecases, but that’s not the point of discussion here.

Let’s directly jump into the main discussion i.e. to validate email in Java using regular expressions.

1. Simple Regex

Regex : ^(.+)@(.+)$

This one is simplest and only cares about ‘@’ symbol. Before and after ‘@’ symbol, there can be any number of characters. Let’s see a quick example to see what I mean.

List emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
emails.add("user1@domain.com");
emails.add("user.name@domain.com");
emails.add("user#@domain.co.in");
emails.add("user@domaincom");

//Invalid emails
emails.add("user#domain.com");
emails.add("@yahoo.com");

String regex = "^(.+)@(.+)$";

Pattern pattern = Pattern.compile(regex);

for(String email : emails){
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Program Output.

user@domain.com : 		true
user@domain.co.in : 	true
user1@domain.com : 		true
user.name@domain.com : 	true
user#@domain.co.in : 	true
user@domaincom : 		true

user#domain.com : 		false
@yahoo.com : 			false

This pattern is available in Common lang’s EmailValidator class. So if it fits your need, you can directly use this class.

2. Adding Restrictions on User Name

Regex : ^[A-Za-z0-9+_.-]+@(.+)$

In this regex, we have added some restrictions on the username part of the email address. Restrictions in the above regex are:

  • [A-Z] characters allowed
  • [a-z] characters allowed
  • [0-9] numbers allowed
  • Additionally email may contain only dot(.), dash(-) and underscore(_)
  • Rest all characters are not allowed

Let’s test some email addresses against the above regex.

List emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
emails.add("user1@domain.com");
emails.add("user.name@domain.com");
emails.add("user_name@domain.co.in");
emails.add("user-name@domain.co.in");
emails.add("user@domaincom");

//Invalid emails
emails.add("@yahoo.com");

String regex = "^[A-Za-z0-9+_.-]+@(.+)$";

Pattern pattern = Pattern.compile(regex);

for(String email : emails){
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Program Output.

user@domain.com : 			true
user@domain.co.in : 		true
user1@domain.com : 			true
user.name@domain.com : 		true
user_name@domain.co.in : 	true
user-name@domain.co.in : 	true
user@domaincom : 			true

@yahoo.com : 				false

Please note that a similar restriction can apply to the domain name part as well. Then regular expression will become like this.

^[A-Z0-9+_.-]+@[A-Z0-9.-]+$

3. Regex for RFC-5322 Validation

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

This regex example uses all the characters permitted by RFC-5322, which governs the email message format. Some of the permitted characters present a security risk if passed directly from user input to an SQL statement, such as the single quote (‘) and the pipe character (|).

You should be sure to escape sensitive characters when inserting the email address into a string passed to another program to prevent security holes such as SQL injection attacks.

List emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
emails.add("user.name@domain.com");
emails.add("user?name@domain.co.in");
emails.add("user'name@domain.co.in");

//Invalid emails
emails.add("@yahoo.com");

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

Pattern pattern = Pattern.compile(regex);

for(String email : emails){
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Program Output.

user@domain.com : 			true
user@domain.co.in : 		true
user.name@domain.com : 		true
user?name@domain.co.in : 	true
user'name@domain.co.in : 	true
@yahoo.com : 				false

4. Restrict Leading, Trailing, or Consecutive Dots

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

Both the local part and the domain name can contain one or more dots, but no two dots can appear right next to each other. Furthermore, the first and last characters in the local part and in the domain name must not be dots:

List emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
emails.add("user.name@domain.com");
emails.add("user'name@domain.co.in");

//Invalid emails
emails.add(".username@yahoo.com");
emails.add("username@yahoo.com.");
emails.add("username@yahoo..com");

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

Pattern pattern = Pattern.compile(regex);

for(String email : emails){
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Program Output.

user@domain.com : 			true
user@domain.co.in : 		true
user.name@domain.com : 		true
user'name@domain.co.in : 	true

.username@yahoo.com : 		false
username@yahoo.com. : 		false
username@yahoo..com : 		false

Now let’s modify the regex such that the domain name must include at least one dot, and that the part of the domain name after the last dot can only consist of letters.

Let’s say domain names are like secondlevel.com or thirdlevel.secondlevel.com. The top-level domain (.com in these examples) must only consist of two to six letters.

Regex : ^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$

List emails = new ArrayList();
emails.add("user@domain.com");
emails.add("user@domain.co.in");
emails.add("user.name@domain.com");
emails.add("user_name@domain.com");
emails.add("username@yahoo.corporate.in");

//Invalid emails
emails.add(".username@yahoo.com");
emails.add("username@yahoo.com.");
emails.add("username@yahoo..com");
emails.add("username@yahoo.c");
emails.add("username@yahoo.corporate");

String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";

Pattern pattern = Pattern.compile(regex);

for(String email : emails){
	Matcher matcher = pattern.matcher(email);
	System.out.println(email +" : "+ matcher.matches());
}

Program Output.

user@domain.com : 				true
user@domain.co.in : 			true
user.name@domain.com : 			true
user_name@domain.com : 			true
username@yahoo.corporate.in : 	true

.username@yahoo.com : 			false
username@yahoo.com. : 			false
username@yahoo..com : 			false
username@yahoo.c : 				false
username@yahoo.corporate : 		false

This last regex is my recommendation for simple email validation in java. Please note that email validation in java without regular expression may be possible, but it is not recommended. Anywhere you need to deal with patterns, regular expressions are your friend.

Please feel free to use this regex as well as edit it as per your application’s additional needs.

Happy Learning !!

Comments

Subscribe
Notify of
guest
13 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode