전체 페이지뷰

2015년 12월 14일 월요일

Java Regex : Validate Email Address

Java Regex : Validate Email Address

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 usecase but that’s not point of discussion here. Let’s directly jump into main discussion i.e. validating email address in java using regular expressions.

Simplest regex to validate emails

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());
}
 
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
Note: This pattern is available in Common lang’s EmailValidator class. So if it fit’s your need, you can directly use this class.

Adding Restrictions on User Name part

Regex : ^[A-Za-z0-9+_.-]+@(.+)$
In this regex, we have added some restriction osn 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());
}
 
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.-]+$

Regex allowing email addresses 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());
}
 
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

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());
}
 
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

Recommended : Regex to restrict no. of characters in top level domain

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());
}
 
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 to validate emails in your project. Please feel free to use this regex as well as edit it as per your application’s additional needs.
Happy Learning !!

댓글 없음:

댓글 쓰기