Java email validation – email regex java

Filed Under: Java

Today we will look into java email validation program. Sometimes we have to validate email address and we can use email regex for that. In the last post, I explained about java regular expression in detail with some examples. In this real-life example, I am trying to validate email addresses using java regular expression.

Java email validation

If you look at the Wikipedia article for valid email address, it supports a lot of special characters. But to cover most of the valid email addresses, I am assuming an email address to consist of characters, digits and special characters +,-,. and _. Also, every email address must contain only one @ symbol.

For the domain name, we can have subdomains also. So a subdomain can contain characters, digits and special characters – and _. Again subdomain can be of multiple levels like xyz.abc.com. So middle-level TLDs must start with a period (.) and it can contain characters, digits and special characters – and _. The last TLD should be of minimum length 2 and can contain words only.

One more important thing to note is that email addresses are case insensitive. So we will use a case insensitive flag to create the pattern object.

Valid email addresses example

Taking all these into account some valid email addresses are:

  • journaldev@yahoo.com, journaldev-100@yahoo.com, journaldev.100@yahoo.com
  • journaldev111@journaldev.com, journaldev-100@journaldev.net,journaldev.100@journaldev.com.au
  • journaldev@1.com,journaldev@gmail.com.com, journaldev+100@gmail.com
  • journaldev-100@yahoo-test.com, journaldev_100@yahoo-test.ABC.CoM

Invalid email addresses example

Now let’s see some invalid email addresses are reason for them to be invalid.

  • journaldev – No @ symbole
  • journaldev@.com.my – Dot after @ symbol
  • journaldev123@gmail.a – last TLD length is less than 2
  • journaldev123@@.com.com – Two @ symbols
  • .journaldev@journaldev.com – ID can’t start with .
  • journaldev()*@gmail.com – invalid special characters in the ID
  • journaldev@%*.com – invalid special characters in the TLD
  • journaldev..2002@gmail.com – ID can’t have two dots
  • journaldev.@gmail.com – ID can’t end with dot
  • journaldev@journaldev@gmail.com – Two @ symbols
  • journaldev@gmail.com.1a – last TLD can have characters only

Email Regex Java Program

So using all the information for email validation in java, I have written the EmailValidator class that can be used to validate email address using a regular expression.


package com.journaldev.util;

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

/**
 * Java email validation program
 * 
 * @author pankaj
 *
 */
public class EmailValidator {
	// Email Regex java
	private static final String EMAIL_REGEX = "^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-z]{2,})$";

	// static Pattern object, since pattern is fixed
	private static Pattern pattern;

	// non-static Matcher object because it's created from the input String
	private Matcher matcher;

	public EmailValidator() {
		// initialize the Pattern object
		pattern = Pattern.compile(EMAIL_REGEX, Pattern.CASE_INSENSITIVE);
	}

	/**
	 * This method validates the input email address with EMAIL_REGEX pattern
	 * 
	 * @param email
	 * @return boolean
	 */
	public boolean validateEmail(String email) {
		matcher = pattern.matcher(email);
		return matcher.matches();
	}
}

Now, we will test this email regex java class using another Class EmailValidatorTest.


package com.journaldev.util;

public class EmailValidatorTest {

	// list of valid email addresses
	private static final String[] validEmailIds = new String[] { "journaldev@yahoo.com", "journaldev-100@yahoo.com",
			"journaldev.100@yahoo.com", "journaldev111@journaldev.com", "journaldev-100@journaldev.net",
			"journaldev.100@journaldev.com.au", "journaldev@1.com", "journaldev@gmail.com.com",
			"journaldev+100@gmail.com", "journaldev-100@yahoo-test.com", "journaldev_100@yahoo-test.ABC.CoM" };

	// list of invalid email addresses
	private static final String[] invalidEmailIds = new String[] { "journaldev", "journaldev@.com.my",
			"journaldev123@gmail.a", "journaldev123@.com", "journaldev123@.com.com", ".journaldev@journaldev.com",
			"journaldev()*@gmail.com", "journaldev@%*.com", "journaldev..2002@gmail.com", "journaldev.@gmail.com",
			"journaldev@journaldev@gmail.com", "journaldev@gmail.com.1a" };

	private static EmailValidator emailValidator;

	public static void main(String args[]) {
		emailValidator = new EmailValidator();
		for (String temp : validEmailIds) {
			boolean valid = emailValidator.validateEmail(temp);
			System.out.println("Email ID " + temp + " is valid? " + valid);
		}
		System.out.println("\n\n");
		for (String temp : invalidEmailIds) {
			boolean valid = emailValidator.validateEmail(temp);
			System.out.println("Email ID " + temp + " is valid? " + valid);
		}
	}
}

The output of the above java email validation example program is shown in the below image.

java email validation, email regex java


Email ID journaldev@yahoo.com is valid? true
Email ID journaldev-100@yahoo.com is valid? true
Email ID journaldev.100@yahoo.com is valid? true
Email ID journaldev111@journaldev.com is valid? true
Email ID journaldev-100@journaldev.net is valid? true
Email ID journaldev.100@journaldev.com.au is valid? true
Email ID journaldev@1.com is valid? true
Email ID journaldev@gmail.com.com is valid? true
Email ID journaldev+100@gmail.com is valid? true
Email ID journaldev-100@yahoo-test.com is valid? true
Email ID journaldev_100@yahoo-test.ABC.CoM is valid? true



Email ID journaldev is valid? false
Email ID journaldev@.com.my is valid? false
Email ID journaldev123@gmail.a is valid? false
Email ID journaldev123@.com is valid? false
Email ID journaldev123@.com.com is valid? false
Email ID .journaldev@journaldev.com is valid? false
Email ID journaldev()*@gmail.com is valid? false
Email ID journaldev@%*.com is valid? false
Email ID journaldev..2002@gmail.com is valid? false
Email ID journaldev.@gmail.com is valid? false
Email ID journaldev@journaldev@gmail.com is valid? false
Email ID journaldev@gmail.com.1a is valid? false

So it seems that our email regex java program is working as expected, you can extend it easily if you want to add some more special characters. That’s all about java email validation example.

You can checkout more regular expressions examples from our GitHub Repository.

Comments

  1. Aparna S Babu says:

    I am getting below error when providing this string “very.(),:;[]\”.VERY.\”very@\ \”very\”.unusual\”@example.com”.
    Can you please help to fix this issue.

    C:\java program>javac pattern.java
    pattern.java:8: error: illegal escape character
    String[] emails = { “\”Fred Bloggs\”@example.com”, “user@.invalid.com”, “Chuck Norris “, “webmaster@mⁿller.de”, “matteo@78.47.122.114”, “simple@example.com”, “very.common@example.com”, “disposable.style.email.with+symbol@example.com”, “other.email-with-hyphen@example.com”, “fully-qualified-domain@example.com”, “user.name+tag+sorting@example.com”, “x@example.com”, “example-indeed@strange-example.com”, “admin@mailserver1”, “example@s.example”, “\” \”@example.org”, “\”john..doe\”@example.com”,”Mark.D’Souza@vicsuper.com.au”,”John..Doe@example.com”,”john.smith@example.com”, “Mark.DSouza@vicsuper.com.au”, “Mark.D.Souza@vicsuper.com.au”, “aparna.s.babu24@gmail.com”, “aparna.babu@cognizant.com”, “aparna.babu23@metlife.com”, “amarnath.rovi@metlife.com”, “aparnababu@cognizant.com”, “wild.wezyr@best-server-ever.com”, “user@[IPv6:2001:db8::1]”, “user@localserver”, “user@com”, “example@s.solutions”, “example@localhost”,”admin@mailserver1″, “example-indeed@strange-example.com”,”x@example.com”, “prettyandsimple@example.com”, “\”very.unusual.@.unusual.com\”@example.com”, “\”much.more unusual\”@example.com”, “very.(),:;[]\”.VERY.\”very@\ \”very\”.unusual\”@example.com”};

    This is my code

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

    public class pattern {
    public static void main(String[] args) {
    Pattern ptr = Pattern.compile(“(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*|(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)*\\<(?:(?:\\r\\n)?[ \\t])*(?:@(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*(?:,@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*)*:(?:(?:\\r\\n)?[ \\t])*)?(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*)|(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)*:(?:(?:\\r\\n)?[ \\t])*(?:(?:(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*|(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)*\\<(?:(?:\\r\\n)?[ \\t])*(?:@(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*(?:,@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*)*:(?:(?:\\r\\n)?[ \\t])*)?(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*)(?:,\\s*(?:(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*|(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)*\\<(?:(?:\\r\\n)?[ \\t])*(?:@(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*(?:,@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*)*:(?:(?:\\r\\n)?[ \\t])*)?(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\”(?:[^\\\”\\r\\\\]|\\\\.|(?:(?:\\r\\n)?[ \\t]))*\”(?:(?:\\r\\n)?[ \\t])*))*@(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*)(?:\\.(?:(?:\\r\\n)?[ \\t])*(?:[^()@,;:\\\\\”.\\[\\] \\000-\\031]+(?:(?:(?:\\r\\n)?[ \\t])+|\\Z|(?=[\\[\”()@,;:\\\\\”.\\[\\]]))|\\[([^\\[\\]\\r\\\\]|\\\\.)*\\](?:(?:\\r\\n)?[ \\t])*))*\\>(?:(?:\\r\\n)?[ \\t])*))*)?;\\s*)”);
    String[] emails = { “\”Fred Bloggs\”@example.com”, “user@.invalid.com”, “Chuck Norris “, “webmaster@müller.de”, “matteo@78.47.122.114”, “simple@example.com”, “very.common@example.com”, “disposable.style.email.with+symbol@example.com”, “other.email-with-hyphen@example.com”, “fully-qualified-domain@example.com”, “user.name+tag+sorting@example.com”, “x@example.com”, “example-indeed@strange-example.com”, “admin@mailserver1”, “example@s.example”, “\” \”@example.org”, “\”john..doe\”@example.com”,”Mark.D’Souza@vicsuper.com.au”,”John..Doe@example.com”,”john.smith@example.com”, “Mark.DSouza@vicsuper.com.au”, “Mark.D.Souza@vicsuper.com.au”, “aparna.s.babu24@gmail.com”, “aparna.babu@cognizant.com”, “aparna.babu23@metlife.com”, “amarnath.rovi@metlife.com”, “aparnababu@cognizant.com”, “wild.wezyr@best-server-ever.com”, “user@[IPv6:2001:db8::1]”, “user@localserver”, “user@com”, “example@s.solutions”, “example@localhost”,”admin@mailserver1″, “example-indeed@strange-example.com”,”x@example.com”, “prettyandsimple@example.com”, “\”very.unusual.@.unusual.com\”@example.com”, “\”much.more unusual\”@example.com”, “very.(),:;[]\”.VERY.\”very@\ \”very\”.unusual\”@example.com”};
    for (String email : emails) {
    System.out.println(email + ” is ” + (ptr.matcher(email).matches() ? “valid” : “invalid”));
    }
    }
    }

  2. Rashid Jorvee says:

    A very simple way to validate the email address using java is this:

    package rashid.jorvee;

    public class ValidEmailValidator {

    public static void main(String[] args) {
    String[] emailAddresses= {“rasj@hds”,”adyu+@sud.com”,”asg_sj-@asgd.co”,”rashid.ta@rasj.com”,”fdh%sdfkj_@dsj.cojn”};
    for(String email : emailAddresses) {
    if(validEmail(email)) {
    System.out.println(“Valid email address :” +email);
    }
    else {
    System.out.println(“Invalid email address :”+email);
    }
    }
    }
    static boolean validEmail(String email) {
    return email.matches(“[a-zA-Z0-9._-][a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}”);
    }
    }

  3. sasha says:

    LOL, why do you initialize static variable in the constructor?

  4. Alessandro says:

    I’ve noticed that the local part of mail address lacks of special characters, according to https://en.wikipedia.org/wiki/Email_address and RFC3696.
    Talking about regex and not Java syntax (I’m not a java programmer), instead of

    [\w-+]

    you should use

    [\w!#$%&’*+/=\-?^_`{|}~]

    Complete regex results:

    ^[\w!#$%&’*+/=\-?^_`{|}~]+(\.[\w!#$%&’*+/=\-?^_`{|}~]+)*@[\w-]+(\.[\w]+)*(\.[a-z]{2,})$

    Hope this helps

  5. Rohit Oza says:

    Hello sir I want to ask you that can we use these above expression in servlet and can then how it is possible please reply me or can use javaqscript and servlet at a time in a one program.

  6. Yasar says:

    You forgot to include capital chars in final group, so the actual regex will be “^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-zA-Z]{2,})$”

    1. Deepak says:

      Can u re poat the complete working example again

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages