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
Rohit J
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?
e6
good pattern but in my case ‘example@-domain.com’ passed, so I added this bit after ‘@’: (?!-)
sonnie
This example was clear and easy to understand ,Thanks for sharing.
Rashid Jorvee
Bjørn T Johansen
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?
Erik
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.
Milos M
I think it’s not limited to 6 chars in the TLD anymore.
Neha
How we can only check @ and . and allow all other characters
Martin
Are U kiddin’ me? com@com is valid? heh
Lokesh Gupta
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
No doubt, these are the perfect examples according to the RFCs.
zain khaliq
It is very Help full for me .
Punya
Regex allowing email addresses permitted by RFC 5322 – this is complete and hard to find anywhere else.
Good explanation.
Thank you