Thursday, 21 December 2017

How to write a regex to validate the email | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=9cfUZ2067pM&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to write a regex to validate the email | Regex in java

EmailValidator.java

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

/**
 *
 * How to validate email address with regular expression
 *
 */

public class EmailValidator
{

    private Pattern pattern;
    private Matcher matcher;

    private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    public EmailValidator()
    {
        pattern = Pattern.compile(EMAIL_REGEX);
    }

    /**
     * Validate hex with regular expression
     *
     * @param email email for validation
     * @return true valid email, false invalid email
     */

    public boolean validate(final String email)
    {
        matcher = pattern.matcher(email);
        return matcher.matches();
    }
}

RegexDemo.java

import java.util.ArrayList;

/**
 *
 * How to validate email address with regular expression
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        ArrayList<String> validEmailList = new ArrayList<>();
        validEmailList.add("peter@yahoo.com");
        validEmailList.add("peter@yahoo.net");
        validEmailList.add("peter.50@yahoo.com");
        validEmailList.add("peter-100@yahoo.com");
        validEmailList.add("peter@gmail.com");
        validEmailList.add("peter+900@oracle.com");

        EmailValidator emailValidator = new EmailValidator();
       
        for (String email : validEmailList)
        {
            System.out.println(email + " is Valid = " + emailValidator.validate(email));
        }

           
        System.out.println("--------------------------------------");
       
        ArrayList<String> inValidEmailList = new ArrayList<>();
        inValidEmailList.add("peteryahoo.com"); // @ Symbol missing
        inValidEmailList.add("peter.50@.yahoo.com"); //. after @ symbol is not allowed
        inValidEmailList.add("peter.@yahoo.com");//. before @ symbol is not allowed
        inValidEmailList.add("peter@gmail@com");// two @ symbols are not allowed
        inValidEmailList.add("peter..900@oracle.com");// two dots are not allowed
       
        for (String email : inValidEmailList)
        {
            System.out.println(email + " is Valid = " + emailValidator.validate(email));
        }  

    }

}

Output

peter@yahoo.com is Valid = true
peter@yahoo.net is Valid = true
peter.50@yahoo.com is Valid = true
peter-100@yahoo.com is Valid = true
peter@gmail.com is Valid = true
peter+900@oracle.com is Valid = true
--------------------------------------
peteryahoo.com is Valid = false
peter.50@.yahoo.com is Valid = false
peter.@yahoo.com is Valid = false
peter@gmail@com is Valid = false
peter..900@oracle.com is Valid = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_email.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_email

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/576579fab6c376d5ecd7cb5b7402b7fd1a7c4ae3/BasicJava/RegexDemo_email/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment