How should I validate an e-mail address?

后端 未结 30 1608
臣服心动
臣服心动 2020-11-22 08:25

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

相关标签:
30条回答
  • 2020-11-22 08:51

    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-

    1. (?!.*?..) "Negative Lookhead" to negate 2 consecutive dots.
    2. [A-Za-z0-9.!#\$\%\&\'*+-/\=\?\^_`{\|}\~]+ Atleast one characters defined. ("\" is used for escaping).
    3. @ There might be one "@".
    4. [A-Za-z0-9]+ then atleast one character defined.
    5. [A-Za-z0-9-.]* Zero or any repetition of character defined.
    6. [A-Za-z0-9]+ Atleast one char after dot.
    0 讨论(0)
  • 2020-11-22 08:53

    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.

    0 讨论(0)
  • 2020-11-22 08:53

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 08:55

    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 ;
    }
    
    0 讨论(0)
  • 2020-11-22 08:56

    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()
    
    0 讨论(0)
  • 2020-11-22 08:57

    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.

    0 讨论(0)
提交回复
热议问题