Languages
[Edit]
EN

Java - validate email with regex

20 points
Created by:
Root-ssh
177980

In this article, we would like to show how to validate e-mail address in Java using regular expressions (Pattern class).

Quick solution:

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

Pattern pattern = Pattern.compile("^[0-9a-zA-Z._%+-]+@[0-9a-zA-Z.-]+\\.[a-zA-Z]{2,}$");

String email = "john@gmail.com";
Matcher matcher = pattern.matcher(email);

if (matcher.matches()) {
    System.out.println("Indicated e-mail is correct.");
} else {
    System.out.println("Indicated e-mail is incorrect!");
}

Output:

Indicated e-mail is correct.

Note: go to the last section to see Google AngualrJS e-mail pattern.

 

Reusable code example

In this section, you can find reusable EmailValidator class.

Program.java file:

package com.example;

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

public class Program {

    public static void main(String[] args) {

        String email = "john@gmail.com";
        String error = EmailValidator.validate(email);

        if (error == null) {
            System.out.println("Indicated e-mail is correct.");
        } else {
            System.out.println(error);
        }
    }
}

EmailValidator.java file:

package com.example;

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

public class EmailValidator {

    private static final Pattern EMAIL_PATTERN = Pattern.compile("^[0-9a-zA-Z._%+-]+@[0-9a-zA-Z.-]+\\.[a-zA-Z]{2,}$");

    private static final String REQUIRED_VALUE_MESSAGE = "E-mail can not be empty.";
    private static final String INCORRECT_FORMAT_MESSAGE = "E-mail format is incorrect.";

    public static String validate(String email) {
        if (email == null || email.isEmpty()) {
            return REQUIRED_VALUE_MESSAGE;
        }
        Matcher matcher = EMAIL_PATTERN.matcher(email);
        if (!matcher.matches()) {
            return INCORRECT_FORMAT_MESSAGE;
        }
        return null;
    }
}

 

AngularJS expression example

Google in AngualrJS suggested its own expression that checks e-mail.

Note: full source code is located here.

The regular expression converted to Java:

Pattern pattern = Pattern.compile("^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$");

Full example:

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

Pattern pattern = Pattern.compile("^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$");

String email = "john@gmail.com";
Matcher matcher = pattern.matcher(email);

if (matcher.matches()) {
    System.out.println("Indicated e-mail is correct.");
} else {
    System.out.println("Indicated e-mail is incorrect!");
}

Alternative titles

  1. Java - validate email with regular expressions
  2. Java - test email with regex
  3. Java - test email with regular expressions
  4. Java - check email with regex
  5. Java - check email with regular expressions
  6. Java - validate e-mail with regular expressions
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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