Languages
[Edit]
EN

Java - validate email address

0 points
Created by:
TylerH
388

In this article, we would like to show how to validate e-mail addresses in Java.

The important thing is: there is more than one way to write expressions that validates e-mail. Some solutions are more precise in checking correctness, others less.

Internet is filled with different solutions, so the article's main goal is to gather all of them in one place.

1. Apache Commons Validator 1.7

import org.apache.commons.validator.routines.EmailValidator;

public class EmailCheckExample {

    static boolean isValidEmail(String emailString) {
        return EmailValidator.getInstance().isValid( emailString );
    }

    public static void main(String[] args) {
        String correctEmailToCheck = "john@gmail.com";
        String wrongEmailToCheck = "1124Sample@ text...";
        String wrongEmailToCheck2 = "example.com";

        System.out.println( isValidEmail( correctEmailToCheck )); // true
        System.out.println( isValidEmail( wrongEmailToCheck ));   // false
        System.out.println( isValidEmail( wrongEmailToCheck2 ));  // false
    }
}

Output:

true
false
false

2. Regular expressions

We have already written about checking email addresses using regular expressions here - Java - validate email with regex.

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join