hello everyone i have one edittext and i would to to write email validation in my Editttext this is a xml code
<EditText
android:id="@+id/mail"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignLeft="@+id/phone"
android:layout_below="@+id/phone"
android:layout_marginRight="33dp"
android:layout_marginTop="10dp"
android:background="@drawable/edit_background"
android:ems="10"
android:hint="E-Mail"
android:inputType="textEmailAddress"
android:paddingLeft="20dp"
android:textColor="#7e7e7e"
android:textColorHint="#7e7e7e" >
</EditText>
and this is a java code
emailInput = mail.getText().toString().trim();
emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
if (emailInput.matches(emailPattern)) {
Toast.makeText(getActivity(), "valid email address",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Invalid email address",
Toast.LENGTH_SHORT).show();
mail.setBackgroundResource(R.drawable.edit_red_line);
}
i can't validation.the toast message is always "Invalid email address" what am i doing wrong? if anyone knows solution please help me
Android Email Validation Simplest way
Assign a String variable to store the value of this EditText:
Use setError in your EditText:
Create a method to check the email:
here is a whole code for login validations......
}
Take a textView ex.checkTextView and validate when the email is valid then the textview is gone otherwise it show the message
Try below code:
Just call below method like,