We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 21 March 2017

Validate Email Address using Java Regular Expression

EmailValidator Regular Expression Pattern
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
Regular ExpressionDescription


for more regular expression syntax refer Java Regular Expression Syntax Descriptions 

EmailValidator.java
package com.javatutorialcorner.regex;

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

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

 public static void main(String[] args) {

  System.out.println("Valid Emails : ");
  
  System.out.println("javatutorialcorner@yahoo.com"+validate("javatutorialcorner@yahoo.com"));
  System.out.println("javatutorialcorner-500@yahoo.com"+validate("javatutorialcorner-500@yahoo.com"));
  System.out.println("javatutorialcorner.500@yahoo.com"+validate("javatutorialcorner.500@yahoo.com"));
  System.out.println("javatutorialcorner111@javatutorialcorner.com"+validate("javatutorialcorner111@javatutorialcorner.com")); 
  System.out.println("javatutorialcorner-500@javatutorialcorner.net"+validate("javatutorialcorner-500@javatutorialcorner.net"));
  System.out.println("javatutorialcorner.500@javatutorialcorner.com.au"+validate("javatutorialcorner.500@javatutorialcorner.com.au"));
  System.out.println("javatutorialcorner@1.com"+validate("javatutorialcorner@1.com"));
  System.out.println("javatutorialcorner@gmail.com.com"+validate("javatutorialcorner@gmail.com.com"));
  System.out.println("javatutorialcorner+500@gmail.com"+validate("javatutorialcorner+500@gmail.com")); 
  System.out.println("javatutorialcorner-500@yahoo-test.com"+validate("javatutorialcorner-500@yahoo-test.com"));
  
  System.out.println("Invalid Emails : ");
  System.out.println("javatutorialcorner@yahoo.com"+validate("javatutorialcorner@yahoo.com"));
  System.out.println("javatutorialcorner-500@yahoo.com"+validate("javatutorialcorner-500@yahoo.com"));
  
  System.out.println("javatutorialcorner – must contains “@” symbol");
  System.out.println("javatutorialcorner@.com.my – tld can not start with dot “.”");
  System.out.println("javatutorialcorner123@gmail.a – “.a” is not a valid tld, last tld must contains at least two characters");
  System.out.println("javatutorialcorner123@.com – tld can not start with dot “.”");
  System.out.println("javatutorialcorner123@.com.com – tld can not start with dot “.”");
  System.out.println("javatutorialcorner@javatutorialcorner.com – email’s first character can not start with dot “.”");
  System.out.println("javatutorialcorner()*@gmail.com – email’s is only allow character, digit, underscore and dash");
  System.out.println("javatutorialcorner@%*.com – email’s tld is only allow character and digit");
  System.out.println("javatutorialcorner..2017@gmail.com – double dots “.” are not allow");
  System.out.println("javatutorialcorner.@gmail.com – email’s last character can not end with dot “.”");
  System.out.println("javatutorialcorner@javatutorialcorner@gmail.com – double “@” is not allow");
  System.out.println("javatutorialcorner@gmail.com.1a -email’s tld which has two characters can not contains digit");

 }

 private static boolean validate(String password) {
  Pattern pattern = Pattern.compile(PATTERN);
  Matcher matcher = pattern.matcher(password);
  return matcher.matches();

 }

}



Output : 

Valid Emails :
javatutorialcorner@yahoo.comtrue
javatutorialcorner-500@yahoo.comtrue
javatutorialcorner.500@yahoo.comtrue
javatutorialcorner111@javatutorialcorner.comtrue
javatutorialcorner-500@javatutorialcorner.nettrue
javatutorialcorner.500@javatutorialcorner.com.autrue
javatutorialcorner@1.comtrue
javatutorialcorner@gmail.com.comtrue
javatutorialcorner+500@gmail.comtrue
javatutorialcorner-500@yahoo-test.comtrue
Invalid Emails :
javatutorialcorner@yahoo.comtrue
javatutorialcorner-500@yahoo.comtrue
javatutorialcorner – must contains “@” symbol
javatutorialcorner@.com.my – tld can not start with dot “.”
javatutorialcorner123@gmail.a – “.a” is not a valid tld, last tld must contains at least two characters
javatutorialcorner123@.com – tld can not start with dot “.”
javatutorialcorner123@.com.com – tld can not start with dot “.”
javatutorialcorner@javatutorialcorner.com – email’s first character can not start with dot “.”
javatutorialcorner()*@gmail.com – email’s is only allow character, digit, underscore and dash
javatutorialcorner@%*.com – email’s tld is only allow character and digit
javatutorialcorner..2017@gmail.com – double dots “.” are not allow
javatutorialcorner.@gmail.com – email’s last character can not end with dot “.”
javatutorialcorner@javatutorialcorner@gmail.com – double “@” is not allow
javatutorialcorner@gmail.com.1a -email’s tld which has two characters can not contains digit

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Validate Email Address using Java Regular Expression Rating: 5 Reviewed By: eHowToNow