2

I have an activity with text edits one for name, email and password and a button that take the user to next activity.

The problem is that even if the user entered a invalid email address he still can go to second activity, I want the email address to be checked than if he entered a valid email address he can go to second activity, if not a toast message appears says INVALID EMAIL ADDRESS.

I am working with KOTLIN but I can work also with java.

Please can anyone guide me on how I can do it ?

Thanks in advance.

  • Try this one- https://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address @hatimboubakri – Asteroid May 07 '18 at 05:29

3 Answers3

2

There is an interesting Java Library that you can use to test if an E-mail is valid or not.

Here is the Gradle dependency:

implementation group: 'commons-validator', name: 'commons-validator', version: '1.6'

Here is how you would validate an e-mail string:

First, capture the value of the editText into a String object. Then you can use the following pattern to capture a Boolean value that tells if the user has entered a Valid E-mail address.

String email = "user@domain.com";

Now, you can use the E-mail Validator:

 boolean isValid = EmailValidator.getInstance().isValid(email);

This is the easiest way I found. I hope that helps!

Alifyz Pires
  • 73
  • 1
  • 7
1
fun isEmailValid(email: String): Boolean {
       return Pattern.compile(
    "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]|[\\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]))|"
 + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$"
 ).matcher(email).matches()
}
0

We have simple Email pattern matcher now

private static boolean isValidEmail(String email) {
        return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }