I am trying to make a form in android in which one of the fields is an email and I wanted to know if there is any way to automatically check if the email entered is correct and instead show something similar to what the following image shows:
I have specified the email field as follows:
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/letraAzul"
android:inputType="textEmailAddress"
android:textSize="@dimen/texto_letra"
android:hint="Email"/>
where android:inputType="textEmailAddress"
it is only in charge of showing a keyboard for the introduction of the email as the documentation says .
If there is no automatic way to check, what would be the best way to check that the email is correct?
Use:
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
- A regular expression of the type
"^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"
(which expression accepts more types of emails?) - Another way
Does anyone know how to do it or have any examples or documentation that can help me?
Cheers
You can use the Pattern class
As you can see we create an EMAIL_ADDRESS pattern and check with the method
matcher
if the email is correct.If you just want to verify that the email is well formed, the class
Pattern
is useful.If you want to know if the email exists, is valid and is not a disposable address or spamtrap mail, the only effective way is to use an external service to validate email. There are quite a few providers, almost all of them through an API with which you can make a REST call, and it returns the result of the email validation.