HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Regular Expressions / Java email validation using regex

Java email validation using regex

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

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

1. Simplest regex to validate email

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 fit’s your need, you can directly use this class.

2. Adding Restrictions on User Name part

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

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

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

Let’s test some email addresses against 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 similar restriction you can apply to domain name part as well. Then regular expression will become like this.

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

3. Java email validation permitted by RFC 5322

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. Among the permitted characters are some that 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, in order 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. Regex to restrict leading, trailing, or consecutive dots in emails

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

5. Regex to restrict no. of characters in top level domain [Recommended]

Now lets modify the regex such that 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 consist of two to six letters only.

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 !!

Reference: http://www.rfc-editor.org/rfc/rfc5322.txt

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Rohit J

    February 19, 2020

    Great Content, just another example of a invalid email, “example.@yahoo.com”, this invalid address hasn’t been handled by any of the above Regex. Can you please help me with this one?

  2. e6

    September 20, 2019

    good pattern but in my case ‘example@-domain.com’ passed, so I added this bit after ‘@’: (?!-)

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

    July 3, 2018

    This example was clear and easy to understand ,Thanks for sharing.

  4. Rashid Jorvee

    June 14, 2018

    package rashid.jorvee;
    
    public class ValidEmailValidator {
    
    	public static void main(String[] args) {
    		String[] emailAddresses= {"rasj@hds","adyu+@sud.com","asg_sj-@asgd.co","rashid.ta@rasj.com","fdh%sdfkj_@dsj.cojn"};
    		for(String email : emailAddresses) {
    			if(validEmail(email)) {
    				System.out.println("Valid email address :" +email);
    			}
    			else {
    				System.out.println("Invalid email address :"+email);
    			}
    		}
    	}
    	static boolean validEmail(String email) {
    		return email.matches("[a-zA-Z0-9._-][a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}");
    	}
    }
  5. Bjørn T Johansen

    August 29, 2016

    Found this and it looked promising but the following email address is not validated, why not?

    name.name@name-names.domain.no

    This is a valid address… What am I missing?

    String EMAIL_PATTERN = " ^[\\w!#$%&’*+/=?`{|}~^-]+(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
    Pattern pattern;
    Matcher matcher;
    matcher = pattern.matcher(o.toString());
    if(!matcher.matches())
    {        
          throw new ValidatorException("");
    }
    
  6. Erik

    August 16, 2016

    The way each example builds upon the prior one was very helpful in understanding the final example. The inclusion of the sample emails for testing made my testing that much easier. Thank you for this.

  7. Milos M

    August 9, 2016

    I think it’s not limited to 6 chars in the TLD anymore.

  8. Neha

    October 28, 2015

    How we can only check @ and . and allow all other characters

  9. Martin

    June 29, 2015

    Are U kiddin’ me? com@com is valid? heh

    • Lokesh Gupta

      June 29, 2015

      Probably you looked up only at first example (there are multiple examples if you scroll down). I have clearly mentioned the rule (only checking @ symbol).. for simplicity purpose. Valid formats are given after that and their rules as well. Try them.

      • Roger

        June 29, 2015

        No doubt, these are the perfect examples according to the RFCs.

  10. zain khaliq

    March 29, 2015

    It is very Help full for me .

  11. Punya

    March 26, 2015

    Regex allowing email addresses permitted by RFC 5322 – this is complete and hard to find anywhere else.
    Good explanation.
    Thank you

Comments are closed on this article!

Search Tutorials

Java Regex Tutorial

  • Java Regex – Introduction
  • Java Regex : Alphanumeric
  • Java Regex : Credit Card Numbers
  • Java Regex : Canadian Postcodes
  • Java Regex : Currency Symbol
  • Java Regex : Date Format
  • Java Regex : Email Address
  • Java Regex : Password
  • Java Regex : Greek Characters
  • Java Regex : ISBNs
  • Java Regex : Min/Max Length
  • Java Regex : No. of Lines
  • Java Regex : No. of Words
  • Java Regex : SSN
  • Java Regex : UK Postal Codes
  • Java Regex : US Postal Codes
  • Java Regex : Trademark Symbol
  • Java Regex : Intl Phone Numbers
  • North American Phone Numbers

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces