Languages
[Edit]
EN

Java - validate email with regex

20 points
Created by:
Root-ssh
115300

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 is required.";
    private static final String INCORRECT_FORMAT_MESSAGE = "Incorrect e-mail format.";

    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!");
}
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