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
Following was used by me. However it contains extra characters than normal emails but this was a requirement for me.
public boolean isValidEmail(String inputString) {
String s ="^((?!.*?\.\.)[A-Za-z0-9\.\!\#\$\%\&\'*\+\-\/\=\?\^_`\{\|\}\~]+@[A-Za-z0-9]+[A-Za-z0-9\-\.]+\.[A-Za-z0-9\-\.]+[A-Za-z0-9]+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inputString);
return matcher.matches();
}
Answer of this question:- Requirement to validate an e-mail address with given points
Explanation-
You can use regular expression to do so. Something like the following.
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
String email = "xyz@xyzdomain.com";
Matcher matcher = pattern.matcher(email);
boolean matchFound = matcher.matches();
Note: Check the regular expression given above, don't use it as it is.
Try this simple method which can not accept the email address beginning with digits:
boolean checkEmailCorrect(String Email) {
if(signupEmail.length() == 0) {
return false;
}
String pttn = "^\\D.+@.+\\.[a-z]+";
Pattern p = Pattern.compile(pttn);
Matcher m = p.matcher(Email);
if(m.matches()) {
return true;
}
return false;
}
For regex lovers, the very best (e.g. consistant with RFC 822) email's pattern I ever found since now is the following (before PHP supplied filters). I guess it's easy to translate this into Java - for those playing with API < 8 :
private static function email_regex_pattern() {
// Source: http://www.iamcal.com/publish/articles/php/parsing_email
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$pattern = "!^$local_part\\x40$domain$!";
return $pattern ;
}
You could write a Kotlin extension like this:
fun String.isValidEmail() =
this.isNotEmpty() && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
And then call it like this:
email.isValidEmail()
The key here is that you want to fully validate the email address. You don’t just want to check it for syntactic correctness, you want to check whether the email address is real.
Two obvious reasons: real users often mis-type their email addresses, and some users may put in fake email addresses. Therefore, you want to do a syntactic check and an existence check.
The best way to do this that I have found on Android is to use the free Cloudmersive Validation API for this.
The code looks like this:
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
EmailApi apiInstance = new EmailApi();
String email = "email_example"; // String | Email address to validate, e.g. \"support@cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try {
FullEmailValidationResponse result = apiInstance.emailFullValidation(email);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EmailApi#emailFullValidation");
e.printStackTrace();
}
I’m using this in all my apps and it is great because I can validate the email addresses in the UX at the point of entry.