Email validation on EditText - Android
Solution 1
We have a simple Email pattern matcher now
Java:
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Kotlin Function:
private fun isValidEmail(email: String): Boolean {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches()
}
Kotlin Extension:
fun String.isValidEmail() =
!TextUtils.isEmpty(this) && Patterns.EMAIL_ADDRESS.matcher(this).matches()
Solution 2
You are mistakenly comparing your EditText
text length to 0, and only if it's true, you do your validation logic.
Here is is the correct code:
@Override
public void onClick(View v) {
String email = editTextEmail.getText().toString();
if(email.length() != 0) {
if (isValidEmail(email)) {
Toast.makeText(getApplicationContext(), "Valid email address!", Toast.LENGTH_SHORT).show();
}
else{
editTextEmail.setError("Email required!");
showAlertValidation();
}
}
else{
editTextEmail.setError("Email required!");
}
}
Solution 3
Its better to make a class which validates the email. So that you can reuse it anywhere you need. You don't even need to put the text of edittext in separate string. Make the following class:
public class EmailValidator {
private Pattern pattern;
private Matcher matcher;
private static EmailValidator sInstance;
public static EmailValidator getInstance() {
if (sInstance == null) {
sInstance = new EmailValidator();
}
return sInstance;
}
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN);
}
public boolean validate(final String hex) {
matcher = pattern.matcher(hex);
return matcher.matches();
}}
Now use this class to check for validation of email of edittext like this:
if(!EmailValidator.getInstance().validate(editTextEmail.getText().toString().trim())){
editTextEmail.setError("Invalid email address");
}
Hope this helps.
Michael Julyus Christopher M.
I'm a student in Bogor Agricultural University, Indonesia. I'm interested in Android, JAVA and Codeigniter. Specialties: Android, JAVA, Web technology (HTML, CSS, JS, Codeigniter), MySQL.
Updated on June 09, 2022Comments
-
Michael Julyus Christopher M. almost 2 years
I have an Email EditText and i want to check it using email validation.
This's my email validation code
public final static boolean isValidEmail(CharSequence target) { if (target == null) { return false; } else { return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } } public void showAlertValidation() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterActivity.this); alertDialog.setTitle("Failed"); alertDialog.setMessage("Invalid Email"); alertDialog.setNegativeButton("Close", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); alertDialog.show(); }
And this is my EditText validation code
editTextEmail= (EditText) findViewById(R.id.editTextEmail); email = editTextEmail.getText().toString(); if(email.length() == 0) { editTextEmail.setError("Email required!"); if (isValidEmail(email)) { Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show(); } else{ showAlertValidation(); } }
The problem is the result of the EditText. When the value of EditText is null, it run
showAlertValidation();
But if the value of EditText is "email" or "email@example" or "[email protected]", it not runshowAlertValidation();
Anything wrong with my code?