Email Verification in Java

3

I am doing a simple test to verify that the user has typed a valid email. So I'm having a syntax error in the "@" invalid AssignmentOperator.

Main class code:

public class Email {

static String email = "[email protected]";

public static void main(String[] args) {

}

public static void validarEmail(){

    //Verifica a posicao do @ no emal.
    int validarEmail = email.indexOf("@");
    //System.out.println(validarEmail);

}

public static void obterNome(){

    //Seleciona o que esta escrito antes do @.
    String obterNome = email.substring(0, 6);
    //System.out.println(obterNome);

}   

Email Verifying Class:

public class VerificaEmail {

public static void main(String[] args) {

    Email e = new Email();

    //Verifica se o Email e valido.
boolean b = e.validarEmail([email protected]);
    if(!b)
        System.out.println("Email Inválido");
    else
        System.out.println("Email Válido");

// Imprime o que esta antes do @.
String nome = e.obterNome([email protected]);
System.out.println(nome);

}
    
asked by anonymous 09.06.2015 / 14:19

3 answers

9

The best way for you to validate an email is to apply a regular expression.

Pattern as a static member

In your Email class, create a static and final private member of type Pattern

private static final String EMAIL_PATTERN = 
        "^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";

private static final Pattern pattern = Pattern.compile(EMAIL_PATTERN, Pattern.CASE_INSENSITIVE);

Use a Matcher within your validate email method

public static boolean validarEmail(String email){
    Matcher matcher matcher = pattern.matcher(email);
    return matcher.matches();
 }

Note that it is best to leave the Pattern as static and already compiled so, you do not need to create a new one with each new validation, you gain in performance

>

Do you need compliance with RFC822?

RFC822 Example

**** Note that I've changed the return of your method to boolen, so it makes more sense for client classes to call your method

    
09.06.2015 / 16:16
2

Does your Email class need a Main method? If you want to record in it informed email a builder would make more sense.

public class Email {

static String email;

public Email(String email){
    this.email = email;
}  

//public static void validarEmail(){
public boolean validarEmail()
    //Verifica a posicao do @ no emal.
    //int validarEmail = this.email.indexOf("@");
    //System.out.println(validarEmail);

    return ( this.email.indexOf('@') > 0 );    
}

public static String obterNome(){

    //Seleciona o que esta escrito antes do @.
    return this.email.substring(0, this.email.indexOf('@'));
    //System.out.println(obterNome);

}

Here in the other class:

public class VerificaEmail {

public static void main(String[] args) {

    Email e = new Email("[email protected]");

    //Verifica se o Email e valido.
    if(!e.validarEmail())
        System.out.println("Email Inválido");
    else
        System.out.println("Email Válido");


    System.out.println(e.obterNome());

}
    
09.06.2015 / 15:50
0

I ended up finding a simpler way to solve my mistakes following the code:

* Email Class:

public class Email {

public static String email = "[email protected]";
public static String Nome;
public static int indiceEmail;

public static boolean validarEmail(){

    //Verifica se o email possui o @.
    indiceEmail = email.indexOf('@');
    if (indiceEmail > 0)
        return(true);
    else
        return(false);  

}

public static String obterNome(){

    if (indiceEmail > 0)
        return Nome = email.substring(0, indiceEmail);
    else
        return("Erro!");        

}   

Main class:

public class VerificaEmail {

public static void main(String[] args) {

    Email e = new Email();      

//Verifica se o Email e valido.
boolean b = e.validarEmail();
    if(!b)
        System.out.println("Email Inválido");
    else
        System.out.println("Email Válido");

// Imprime o que esta antes do @.
String nome = e.obterNome();
System.out.println(nome);

}

In this way it was simpler and better to understand and without altering the intention of the author.

    
09.06.2015 / 21:28