0

I'm new in Android programming and Java and it's very difficult to understand.

I have an editText field for the email that a user can enter. I get the input with this:

final EditText loginEmail = (EditText) findViewById(R.id.loginEmail);

No I'm checking if this field is empty or not. I'm doing this with this little if:

if (loginEmail.getText().toString().length() == 0) {
  loginEmail.setError("Please enter a E-Mail Address!");
} else if () {
  loginEmail.setError("Invalid E-Mail Address!");
} else {
  //Do a thing if is valid...  
}

After the first if I want to check if the input is a valid email or not, but don't know how to deal with this.

I've tried it with the answer from this post: Email Address Validation in Android on EditText

but I don't know how to use this method...

Thanks for helping me!

Community
  • 1
  • 1
ItsOdi1
  • 219
  • 3
  • 22

7 Answers7

3

Use :

if (!Patterns.EMAIL_ADDRESS.matcher(loginEmail.getText().toString()).matches()){
    loginEmail.setError("Please enter a Valid E-Mail Address!");
}else {
    //email is valid
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
1

Add the method from the other post to your class. Then use this:

String email = loginEmail.getText().toString();
if (email.isEmpty()) {
    loginEmail.setError("Please enter a E-Mail Address!");
} else if (!isValidEmail(email)) {
    loginEmail.setError("Invalid E-Mail Address!");
} else {
  // Do a thing if is valid...  
}
  • 2
    The other answer shows the short way; mine shows how to use another method so I'll leave it here for now. –  Jan 12 '17 at 14:19
1

Try this,

String email_string = loginEmail.getText().toString();
if(!TextUtils.isEmpty(email_string) && android.util.Patterns.EMAIL_ADDRESS.matcher(email_string).matches()) {
    //valid emailID
}
Mujammil Ahamed
  • 1,454
  • 4
  • 24
  • 50
1

Check if entered text is empty and match with email pattern.

String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

String email = loginEmail.getText().toString();

if (email.isEmpty() && email.matches(emailPattern)) {
    loginEmail.setError("Please enter a E-Mail Address!");
} else 
    loginEmail.setError("Invalid E-Mail Address!"); 
}
santosh kumar
  • 2,952
  • 1
  • 15
  • 27
0

WORKING !!!!

We have simple Email pattern matcher now

private static boolean isValidEmail(String email) {
        return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }
Salman Nazir
  • 2,759
  • 2
  • 28
  • 42
0

Source: This is the code from one of the StackOverflow Answer for email validation using java.

public static boolean isEmailValid(String email)
    {
        Pattern pattern;
        Matcher matcher;
        final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email);
        return matcher.matches();
    }
0

Declare this string format globally

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

Now match the patter from text

if ((!email_a.getText().toString().trim().matches(EMAIL_PATTERN))) { email_a.setError("Invalid email"); } Enjoy Coding :)