Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to validate email 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));
    }
}
 
PREVIOUS NEXT
Tagged: #validate #email #java
ADD COMMENT
Topic
Name
8+5 =