Как проверить электронную почту Java
import java.util.regex.Pattern;
public class EmailValidation {
// Creating The actual Method
public String validateEmail(String email) {
if (email == null || email.isEmpty()) {
return "Nothing to be validated";
}
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." +"[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
Pattern pattern = Pattern.compile(emailRegex);
if (pattern.matcher(email).matches()) {
return "It's valid";
}
return "Email not valid";
}
// Everything below this point is for testing that it works
public static void main(String[] args) {
EmailValidation emailValidator = new EmailValidation();
String email1 = "ola.Nordmann@online.no";
String email2 = "foobar";
System.out.println("Email1: " + email1 + " " + emailValidator.validateEmail(email1));
System.out.println("Email2: " + email2 + " " + emailValidator.validateEmail(email2));
}
}
Plain Panda