Java email validation - email regex java With Examples

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:

Invalid email addresses example

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

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[] { "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]" };
	// list of invalid email addresses
	private static final String[] invalidEmailIds = new String[] { "journaldev", "[email protected]",
			"[email protected]", "[email protected]", "[email protected]", "[email protected]",
			"journaldev()*@gmail.com", "[email protected]%*.com", "[email protected]", "[email protected]",
			"[email protected]@gmail.com", "[email protected]" };
	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("nn");
		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 [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID [email protected] is valid? true
Email ID journaldev is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID journaldev()*@gmail.com is valid? false
Email ID [email protected]%*.com is valid? false
Email ID [email protected] is valid? false
Email ID [email protected] is valid? false
Email ID [email protected]@gmail.com is valid? false
Email ID [email protected] 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.

By admin

Leave a Reply

%d bloggers like this: