How do I check an email address for validity?

I have two options in the app for getting email mail (for now anyway): this is to get it from the server as a response to my request (the server is mine and therefore comes a lot of things other than mail, and mail has to be pulled separately) or to force the user to enter it (address) manually in the input field. What is one that the second option requires the implementation of address validation, that is, for example, we still got the address, and we have a string, and here is the question of how to do this validation. For example, I pull the email address from the device memory, where I save it after the request:

sp = getSharedPreferences("userInfo", 0);
String applicant_email = sp.getString("applicant_email", "");

Or here's how I can get it from the input field:

String c = mail_of_sender.getText().toString();

Where, mail_of_sender is the input field. And now the actual question is-how is validation implemented and how to do it at all? I hope I'm not the first person to face such a question and I think that there is already a solution to my problem.

Author: Эникейщик, 2018-10-02

1 answers

Kotlin:

/**
 * Check the text in EditText on validity of email
 * @return isValid
 */
internal fun EditText.isEmailValid(): Boolean {
    return android.util.Patterns.EMAIL_ADDRESS.matcher(this.text.toString()).matches()
}

Java:

 public boolean isEmailValid(String email){
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }
 8
Author: RomanK., 2018-10-02 10:21:11