0

I have a regular expression to validate an email and when I run it, it constantly tells me that its an invalid email address but the email is correct

email = (EditText) findViewById(R.id.email);   

final String Email = email.getText().toString();

if (!Email.matches("^([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})$"))
     {
         email.requestFocus();
         email.setError("INVALID EMAIL ADDRESS");
     }

Does anyone know it would give me the error even though it is correct?

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
Michelle
  • 89
  • 1
  • 10

1 Answers1

0

try this instead. this should remove any hidden spaces/characters after the email.

Email.matches
    ("([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5}).*"))

according to http://developer.android.com/reference/java/lang/String.html

public boolean matches (String regularExpression)

Added in API level 1 Tests whether this string matches the given regularExpression. This method returns true only if the regular expression matches the entire input string. A common mistake is to assume that this method behaves like contains(CharSequence); if you want to match anywhere within the input string, you need to add .* to the beginning and end of your regular expression. See matches(String, CharSequence).

Angel Koh
  • 12,479
  • 7
  • 64
  • 91