What\'s a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn\'t seem to be
Can I STRONGLY recommend you don't try to 'validate' email addresses, you'll just get yourself into a lot of work for no good reason.
Just make sure what is entered won't break your own code - e.g. no spaces or illegal characters which might cause an Exception.
Anything else will just cause you a lot of work for minimal return...
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()
Since API 8 (android 2.2) there is a pattern: android.util.Patterns.EMAIL_ADDRESS http://developer.android.com/reference/android/util/Patterns.html
So you can use it to validate yourEmailString:
private boolean isValidEmail(String email) {
Pattern pattern = Patterns.EMAIL_ADDRESS;
return pattern.matcher(email).matches();
}
returns true if the email is valid
UPD: This pattern source code is:
public static final Pattern EMAIL_ADDRESS
= Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
refer to: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/util/Patterns.java
So you can build it yourself for compatibility with API < 8.
There is a Patterns
class in package android.util
which is beneficial here. Below is the method I always use for validating email and many other stuffs
private boolean isEmailValid(String email) {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Call This Method where you want to validate email ID.
public static boolean isValid(String email)
{
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches())
{
return true;
}
else{
return false;
}
}
Here is android.util.Patterns.EMAIL_ADDRESS
[a-zA-Z0-9+._\%-+]{1,256}\@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}(.[a-zA-Z0-9][a-zA-Z0-9-]{0,25})+
String
will match it if
Start by 1->256 character in (a-z, A-Z, 0-9, +, ., _, %, - , +)
then 1 '@' character
then 1 character in (a-z, A-Z, 0-9)
then 0->64 character in (a-z, A-Z, 0-9, -)
then **ONE OR MORE**
1 '.' character
then 1 character in (a-z, A-Z, 0-9)
then 0->25 character in (a-z, A-Z, 0-9, -)
Example some special match email
a@b.c
a+@b-.c
a@b.c.d.e.f.g.h
You may modify this pattern for your case then validate by
fun isValidEmail(email: String): Boolean {
return Patterns.EMAIL_ADDRESS.matcher(email).matches()
}