1

I'm trying to write if condition that checks whether an email address entered from user is valid email.

How can I write it?

Please, I want a simple way and not complicated

my method is

  public void setStudentEMail(String StudentEMail)
{
     this.StudentEMail=StudentEMail;    
}
 
  public String getStudentEMail()
{
    return this.StudentEMail;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Haneen
  • 41
  • 5
  • 2
    Are you using a JakartaEE-compatible server? If so, we can use [Bean Validation](https://beanvalidation.org/) and [`@Email`](https://jakarta.ee/specifications/platform/8/apidocs/javax/validation/constraints/email) – Turing85 Apr 03 '21 at 17:45
  • 2
    Does this answer your question? [What is the best Java email address validation method?](https://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method) – Davide D'Alto Apr 03 '21 at 17:53
  • no I'm not use it – Haneen Apr 03 '21 at 17:54

2 Answers2

2

You can use official email package of Java https://javaee.github.io/javamail/ . It has a built-in method to validate email address. You don't have to maintain a regex on your own.

public boolean isValidEmail(String email) {
   boolean valid = true;
   try {
      InternetAddress emailAddr = new InternetAddress(email);
      emailAddr.validate();
   } catch (AddressException e) {
      valid = false;
   }
   return valid;
}
aatwork
  • 2,130
  • 4
  • 17
  • The site you link is outdated, the newest is - after rebranding to JakartaMail and moving to the Eclipse Foundation - on https://eclipse-ee4j.github.io/mail/ – Mark Rotteveel Apr 04 '21 at 06:19
0

A simple, but not perfect method is

public boolean isValid(String email) {
    if (email == null) return false;
    int at = email.indexOf("@");
    if (at < 0) return false;
    int dot = email.lastIndexOf(",");
    return at < dot;
}

However, you will need a much more complex approach to have this working perfectly, like

public static boolean isValid(String email)
{
    String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
                        "[a-zA-Z0-9_+&*-]+)*@" +
                        "(?:[a-zA-Z0-9-]+\\.)+[a-z" +
                        "A-Z]{2,7}$";
                          
    Pattern pat = Pattern.compile(emailRegex);
    if (email == null)
        return false;
    return pat.matcher(email).matches();
}

https://www.geeksforgeeks.org/check-email-address-valid-not-java/

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • The local part of the e-mail can also contain many other characters according to the [RFC 5322](https://tools.ietf.org/html/rfc5322#section-3.2.3): `[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]`. Of course in practice many sites fail with characters such as `$` or `'` in the local part. – Piotr P. Karwasz Apr 04 '21 at 05:54
  • But the question explicitly stated _"I want a simple way and not complicated"_!!! Writing code to validate email addresses instead of using a proven API seems inappropriate (unless there are special validation requirements). – skomisa Apr 04 '21 at 06:14