-4

I'm trying to create a regular expression to check to see if a valid email address has been entered. There is something wrong with my regular expression. Here is the source code I'm using:

if (!Pattern.matches("^[\\w-\\+]+(\\.[\\w]+)*@[\\w-]+(\\.[\\w]+)*(\\.[a-z]{2,})$", s)) {
                    et.setError("Enter a valid Email Address");
}

What am I doing wrong?

SirDarius
  • 41,440
  • 8
  • 86
  • 100
user268397
  • 1,917
  • 7
  • 38
  • 56
  • 2
    The only real validation you should do with regex is to check for a "@". There's a billion questions on this topic already... and a very valid reason why you shouldn't overcomplicate this. – tenub Feb 05 '14 at 20:10
  • @tenub Other than the fact that Android has a collection of built-in patterns to use, why would you only check for `@` in an email validation regex? – admdrew Feb 05 '14 at 20:12
  • 2
    *What am I doing wrong?* To be blunt...using regex to validate an email address. [It's ugly, unmanageable, and difficult to spot errors in.](http://ex-parrot.com/~pdw/Mail-RFC822-Address.html) Bite the bullet and do more in-depth String parsing and analysis to see if the email is truly valid. – Makoto Feb 05 '14 at 20:18
  • `using regex to validate an email address` Well, that's precisely how [`android.util.Patterns.EMAIL_ADDRESS` works](https://android.googlesource.com/platform/frameworks/base/+/cd92588/core/java/android/util/Patterns.java). – admdrew Feb 05 '14 at 20:26

3 Answers3

1
public final static boolean isValidEmail(CharSequence target) {
    return ((!TextUtils.isEmpty(target)) && (android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()));
} 

You can see more default patterns Patterns | Android Developers

Triode
  • 11,309
  • 2
  • 38
  • 48
  • So how do you know they're using the Android SDK or the `android.util` package? – Makoto Feb 05 '14 at 20:19
  • since the question is tagged with android – Triode Feb 05 '14 at 20:20
  • Does the package know about me@you.email, or me@mail.würzburg. – Hannes Feb 05 '14 at 20:20
  • 1
    why the down vote ? Would greatly appreciate if the down voter would have provided the reason – Triode Feb 05 '14 at 20:22
  • @Hannes [Test for it yourself!](https://android.googlesource.com/platform/frameworks/base/+/cd92588/core/java/android/util/Patterns.java) – admdrew Feb 05 '14 at 20:25
  • @admdrew No can do. Only a wp7 available. My point is, that regular expressions won't help with emails. You can litteraly have any email. I had a .info mail address that failed many tests. I had email address es with localized chars like French and German ones. Now we have new TLD in the pipe like .sport, .münchen, ... If you build for business you cannot exclude any valid email address, or you will exclude a paying customer. – Hannes Feb 05 '14 at 20:30
  • @Hannes `No can do. Only a wp7 available` That link shows you the actual regex that's used by `android.util.Patterns.EMAIL_ADDRESS`, so testing it is platform agnostic. – admdrew Feb 05 '14 at 20:32
  • @Hannes `You can litteraly have any email` Not quite, that's what the RFC is for. `If you build for business you cannot exclude any valid email address, or you will exclude a paying customer.` The point in validating input is to prevent issues with that data down the line (for that pesky user who think their address is `DROP ALL TABLES;`). – admdrew Feb 05 '14 at 20:34
  • 1
    @admdrew you don't like little bobby tables? ;) – Hannes Feb 05 '14 at 20:35
  • @Hannes haha! I just don't want him as a customer ;) – admdrew Feb 05 '14 at 20:36
  • @admdrew Wow. I don't want to change this crap of code. I cannot verify that all TLDs are in there, but I will test it tomorrow. – Hannes Feb 05 '14 at 20:37
  • @Hannes I just glanced at the regex, but it doesn't appear to validate specific TLDs, it just makes sure that the TLD portion of the address is no more than 25 characters. – admdrew Feb 05 '14 at 20:38
  • @Hannes It's a short blurb, just search for `public static final Pattern EMAIL_ADDRESS`. – admdrew Feb 05 '14 at 20:39
  • @admdrew I saw a chain for some more interesting chars encoded in Unicode notion. I'll have a run tomorrow. – Hannes Feb 05 '14 at 20:41
  • @Hannes Ahh cool, let me know. It's nice to be able to localize stuff. – admdrew Feb 05 '14 at 20:42
  • http://www.rubular.com/ try it out – Triode Feb 05 '14 at 20:42
  • The regular expression from google's android helper class looks pretty good. Short and rfc compliant. But no none ASCII chars. – Hannes Sep 19 '14 at 13:47
1

I do not know if your intent is to play with regex, but Android has its own built in method to validate email adress

boolean isValid = android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); 

it is available since Gingerbread

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
-1

This did the trick:

([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})
user268397
  • 1,917
  • 7
  • 38
  • 56