How to validate Email Address in Java

Hey Everyone! In this article, we will see how we can validate an Email address provided by the user in Java.

Email Address Validation in Java

Email Address in Java can be validated by using Regular Expressions by providing the appropriate pattern.
For this, the Pattern and Matcher classes are used that are present in the java.util.regex; package.

The format for a valid Email Address is as follows:

  • Should start with characters, digits or symbols like ‘-‘,’_’ ‘.’ symbols.
  • It must contain only one ‘@’ character.
  • The domain name can contain characters and digits.
  • The domain name is followed by a ‘.’ and after that it can contain characters and digits.

Thus by considering the above format the following Regular Expression can be applied to validate a String.

^[a-zA-Z0-9_#$%&’*+/=?^.-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$

Lets breakdown the above expression to understand it
1. ^[a-zA-Z0-9_#$%&’*+/=?^.-]+
Here
^ – Indicates start of the string where the characters can be anywhere between a-z A-Z digits 0-9 and some allowed special symbols like ‘_’ ‘#’ ‘?’ etc.
+ – Indicates One or More.

2. (?:\\.[a-zA-Z0-9_+&*-]+)*
() – Capturing Group.
? – Indicates Zero or More.

3. @(?:[a-zA-Z0-9-]+\\.)+
The name is followed by the ‘@’ symbol.
\ – Is used to escape a character.




4. [a-zA-Z]{2,7}$
[] – Character set.
{2,7} – Matches the preceded characters at least 2 times but not more than 7 times i.e length of top level domain.
$  – End of the String

Now let’s have a look at the program.

Java Program for Email Validation using Regular Expressions

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


public class EmailValidation {

    public static void main(String[] args) 
    {
        final String email1 = "[email protected]"; //valid email
        final String email2 = "Abcd_023gmail.com";      //invalid email no @ symbol
        final String email3 = "[email protected]"; //invalid email as . symbol is after @
        
        
        System.out.println(email1+" Validation status "+emailValidation(email1));
        System.out.println(email2+" Validation status "+emailValidation(email2));
        System.out.println(email3+" Validation status "+emailValidation(email3));
        
        
    }
    
    public static boolean emailValidation(String email)
    {
        String email_pattern = "^[a-zA-Z0-9_#$%&’*+/=?^.-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        Pattern pat = Pattern.compile(email_pattern);
        Matcher mat = pat.matcher(email);
        return mat.matches();
    }
    
}
Output:

[email protected] Validation status true
Abcd_023gmail.com Validation status false
[email protected] Validation status false

The regular expression pattern is passed as a parameter in the compile method which returns a reference to the Pattern class.
Using this reference the email address to be validated is passed as a parameter in the matcher method of the Matcher class which returns either true or false.
In the above program, email1 is in a valid format.
Email2 does not contain the ‘@’ symbol which is not a valid email address format.
Email3 contains a ‘.’ character after the ‘@’ symbol which is not in a valid format.


I hope this article was helpful to you to learn how to validate email address in Java.
If you have any doubt or suggestion leave a comment down below.

Also learn:


Leave a Reply

Your email address will not be published. Required fields are marked *