-1

Does anyone know what the exact regular expression of androids patterns.EMAIL_ADDRESS uses?

I need to use the same pattern for PHP. Thank you.

nerotech
  • 49
  • 1
  • 7
  • If you need to validate emails in general and you're just looking for a regular expression and not that specific one for a particular reason, you may want to look at this: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – nonzaprej May 18 '17 at 07:40
  • 1
    Possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – Dymen1 May 18 '17 at 07:43
  • I have not yet found a good regex that let's all real emails through. Because of that. Let the user type it twice and if he/she fails twice, it's not meant to be... – Andreas May 18 '17 at 07:55

3 Answers3

2

this is how PHP filter email

$Email = "dev@gmail.com";

if (filter_var($Email, FILTER_VALIDATE_EMAIL))
    echo "Email Is Valid";
else
    echo "Email Isn't Valid";
A Dev
  • 82
  • 1
  • 10
  • Does FILTER_VALID_EMAIL have the same regular expression as EMAIL_ADDRESS? I would like to know EMAIL_ADDRESS regex definition. Thanks – nerotech May 18 '17 at 07:49
  • @nerotech: RegEx don't differ that much from one environment to another, so use what you've got. Or follow the advice of **A Dev** and use `filter_var()`, it is a correct answer to your question. – KIKO Software May 18 '17 at 08:11
  • @nerotech, they both use same standard so, don't worry about result – A Dev May 18 '17 at 10:55
0
 public boolean checkEmail(EditText editText) {
    boolean emailIsValid = editText.getText().toString().matches("[^.]?([a-zA-Z0-9]+[._]?)*[-]?([a-zA-Z0-9]+[._]?)*[a-zA-Z0-9][@][a-z0-9]*[-]?[a-z0-9]*[.][a-z0-9]*[.]?[a-z0-9]*");
    return !(android.util.Patterns.EMAIL_ADDRESS.matcher(editText.getText().toString()).matches()) || !emailIsValid;
}

if it returns true then it is not a valid email.

Anmol317
  • 1,356
  • 1
  • 14
  • 31
0

checkEmail method will return true, if email text is valid.

public final static Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(

            "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                    + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                    + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                    + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                    + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                    + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$");


    public static boolean checkEmail(String email) {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
Harshal Shah
  • 427
  • 3
  • 5
  • Please, have a look at these sites: TLD list https://www.iana.org/domains/root/db valid/invalid addresses https://en.wikipedia.org/wiki/Email_address#Examples regex for RFC822 email address http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto May 19 '17 at 12:19