2

I am developing Android application with email validation, for which I am using Regex, which is attached below:

It works in certain conditions, but for certain conditions, it gives me App Not Responding errors.

For example, if I put some text with some specific length, but no @ , then it gives me ANR. If my email length is below that range it gives me proper validation error. My regex looks like:

 public static final String EMAIL_PATTERN ="(?:[A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]*)+(\\w[A-Za-z0-9!#$%&'*+-/=?^_`{|}~.]+)@(?:[A-Za-z0-9-.]*)";

// Here it gives proper validation error  for fgefjkbgjerk.com but gives ANR 
   for 
   sjkfghhrghergfhfgfghkfgkfgkjkgergejkgrjekfghfghfg.com 

public static boolean checkEmailValidations(String regex,String email){
    boolean patternStatus=false;
    Pattern pattern= Pattern.compile(regex);
    Matcher matcher = pattern.matcher(email);
    if( matcher.matches()){
        patternStatus=false;
    }else{
        patternStatus=true;
    }
    return patternStatus;
}

Is there a problem with the Regex or my implementation?

ValarMorghulis
  • 79
  • 1
  • 13
nilkash
  • 7,408
  • 32
  • 99
  • 176
  • When you get ANR, what does your Logcat stacktrace say? – Kapil G Jul 28 '17 at 05:57
  • Your pattern looks erroneous. Check and validate your Regex at https://regex101.com – Kapil G Jul 28 '17 at 05:58
  • Yeah I checked it. as per suggection it should be like this `(?:[A-Za-z0-9!#$%&'*+-\/=?^_`{|}~.]*)+(\\w[A-Za-z0-9!#$%&'*+-\/=?^_`{|}~.]+)@(?:[A-Za-z0-9-.]*)`. I tried this one as well. but same result. – nilkash Jul 28 '17 at 06:01
  • Check my answer below. If you just want to match standard email with no custom requirements you can use standard Android android.util.Patterns – Kapil G Jul 28 '17 at 06:09

2 Answers2

1

If you just want to validate an email you can use Android patterns

private boolean isValidEmail(String email) {
    Pattern pattern = Patterns.EMAIL_ADDRESS;
    return pattern.matcher(email).matches();
}

The pattern they use is -

    public static final Pattern EMAIL_ADDRESS        
= Pattern.compile(            
           "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + 
           "\\@" + 
           "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + 
           "(" + "\\." +
           "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + 
           ")+" 
       );

So you can modify that as well according to your needs.

Refer here and Android email pattern

Kapil G
  • 4,081
  • 2
  • 20
  • 32
0

I believe you should use a pattern like this:

public static final Pattern  EMAIL_PATTERN = 
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

And If you're not sure about your Matcher, i always use this pattern:

 public static boolean checkEmailValidations(String email) {
            Matcher matcher = EMAIL_PATTERN .matcher(email);
            return matcher.find();
}
  • 1
    Hi samed thank you for quick replay. I have some extra requirement to allow certain character and avoid some characters. That the reason I have modified my regex. – nilkash Jul 28 '17 at 06:07