0

I had implemented email validation in my app but problem is that app was accepting local domain part of more than 64 character and when I write ..nnn..@gmail.com app was accepting this also and also As per the RFC 3696 specification #section 3, period (".") may not be used to start or end the local part, nor may two or more consecutive periods appear.

Please help me out

private boolean isValidEmail(String email) {// validation for email Id
    boolean isValid = false;
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,255}$";

    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(email);
    if (matcher.matches()) {
        isValid = true;

    }
    return isValid;
}
Raghav
  • 137
  • 5
  • 11

7 Answers7

6

The Android SDK has a built-in regular expression for checking Emails.

Patterns JavaDoc

Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(email);
return matcher.matches();
Christopher
  • 9,682
  • 7
  • 47
  • 76
4

try this code. Return true if email is valid.

android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
AmmY
  • 1,821
  • 1
  • 20
  • 31
2
public final static boolean isValidEmail(CharSequence target) {
    if (TextUtils.isEmpty(target)) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}
1

try this,

String expression = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
Niroj
  • 1,114
  • 6
  • 29
1

try this one

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidation {
private Pattern pattern;
private Matcher matcher;

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,})$";

public EmailValidation() {
    pattern = Pattern.compile(EMAIL_PATTERN);
}

public boolean isValidate(final String hex) {
    matcher = pattern.matcher(hex);
    return matcher.matches();
  }
}
anu
  • 213
  • 1
  • 3
  • 10
1

I would suggest an easy solution among all these answer

public static boolean isEmail(String email){
  return android.util.Patterns.EMAIL_ADDRESS.matcher(inputData.trim()).matches()
}

Read this documentation about Patterns.

droidev
  • 7,352
  • 11
  • 62
  • 94
0

i would suggest you checkout this library for android client side validation

https://github.com/ragunathjawahar/android-saripaar

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45