Java code to validate Email in Java using regex (Regular Expression)

The following code illustrates the code for validating Email using Java Regular Expression (java.util.regex). Before writing code for validating Email Address, we have to understand which is valid email. There is no fixed rules for deciding the valid emails. The following conditions may be applied for validating email.

Email addresses may include

1. Alphanumeric Characters ( i.e Uppercase letters (A-Z) , Lowercase Letters ( a–z), Digits (0-9) )

2. Special Characters ! # $ % & ‘ * + – / = ? ^ _ { | } ~ ( Note : Email Name may start with (A-Z, a-z, 0-9) )

3. Character . (dot) which is not the first or last character,also it does not appear consecutively (e.g. xyz..abc@test.com).

4. Only One at sign @ followed by a valid Domain Name. Valid Domain Name starts with Alphanumeric character , allows special character - , should have at least one .(dot) followed by at least two to maximum of six letters (eg. in ,org, com)

The following Pattern may be used for validating the E-Mail Address.


^([\w]((\.(?!\.))|[-!#\$%'\*\+/=\?\^</code>\{\}\|~\w])*)(?&lt;=[\w])@(([\w][-\w]*[\w]\.)+[a-zA-Z]{2,6})$

//For Better understanding of the above Pattern , we can split into various parts in order.
Pattern starts with ^ and ends with $. ^ matches starts from the beginning. \w is predefined character class for a word character: [a-zA-Z_0-9]

1. [\w] -The first character of the Email matches any alphabetic character from a to z or A to Z or any numeric character from 0 to 9.

2.  (\.(?!\.)) -If the next character is a dot (.), matches it. If it is not a dot, looks for next character and continue the match. (?!\\.) ensures, there should not be two or more dot (.) consecutively

3. |[-!#\$%'\*\+/=\?\^\{\}\|~\w])*  - Match any word character or one of the characters: -!#$%'*+=?^{}|~.

4. The step 2 & 3 pattern is matched zero or more times (due to *)

5.  (?<=[\w])@  - If the character that precedes the @ character is A through Z, a through z, or 0 through 9, then it continues the match. After that matches the @ character.

6.  (([\w][-\w]*[\w]\.)+[a-zA-Z]{2,6})  - Validates the Domain Name. Match one character with a value of A-Z, a-z, or 0-9, followed by 0 or more occurrences of a word character or a hyphen, followed by an alphanumeric character, followed by a period and this pattern is repeated one or more times, and must be followed by 2 to 6 alphabetic (a-z, A-Z) characters.

The following lines of code is used to validate the Email Address which accepts the Email Address through Command Line. */


package com.javaonline;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidation{
public static void main(String args[])
{
String[] email = {"javaonline@gmail.com","m.@server1.xyz.com", "xyz..abc@test.in","xyz.abc@test.in9", "xyz_abc@test.in"};

for (int i=0;i<=email.length-1;i++)
System.out.println("Email " + email[i] +" is " + (isValidEmail(email[i])?"Valid":"Not Valid"));

}

private static boolean isValidEmail(String emailAddress)
{
Pattern pattern=Pattern.compile("^([\\w]((\\.(?!\\.))|[-!#\\$%'\\*\\+/=\\?\\^`\\{\\}\\|~\\w])*)(?<=[\\w])@(([\\w][-\\w]*[\\w]\\.)+[a-zA-Z]{2,6})$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(emailAddress);
return matcher.matches();
}
}

Running the above program

Output :

email validation output

 

Reason for not valid:

m.@server1.xyz.com  - Dot is before @

xyz..abc@test.in  -  Consecutive dots are not allowed

xyz.abc@test.in9  -  Digit is there in the last of the domain name.

 

You may also like

Leave a Reply

Be the First to Comment!