我应该如何验证电子邮件地址?

本文翻译自:How should I validate an e-mail address?

What's a good technique for validating an e-mail address (eg from a user input field) in Android? 在Android中验证电子邮件地址(例如来自用户输入字段)的好技术是什么? org.apache.commons.validator.routines.EmailValidator doesn't seem to be available. org.apache.commons.validator.routines.EmailValidator似乎不可用。 Are there any other libraries doing this which are included in Android already or would I have to use RegExp? 是否有其他库已包含在Android中,或者我必须使用RegExp?


#1楼

参考:https://stackoom.com/question/7dF0/我应该如何验证电子邮件地址


#2楼

Since API 8 (android 2.2) there is a pattern: android.util.Patterns.EMAIL_ADDRESS http://developer.android.com/reference/android/util/Patterns.html 从API 8(android 2.2)开始,有一种模式:android.util.Patterns.EMAIL_ADDRESS http://developer.android.com/reference/android/util/Patterns.html

So you can use it to validate yourEmailString: 因此,您可以使用它来验证yourEmailString:

private boolean isValidEmail(String email) {
    Pattern pattern = Patterns.EMAIL_ADDRESS;
    return pattern.matcher(email).matches();
}

returns true if the email is valid 如果电子邮件有效,则返回true

UPD: This pattern source code is: UPD:此模式源代码是:

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 请参阅: 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. 因此,您可以自己构建它以与API <8兼容。


#3楼

email is your email-is. 电子邮件是您的电子邮件。

public boolean validateEmail(String email) {

    Pattern pattern;
    Matcher matcher;
    String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    pattern = Pattern.compile(EMAIL_PATTERN);
    matcher = pattern.matcher(email);
    return matcher.matches();

    }

#4楼

    public boolean isValidEmail(String email)
{
    boolean isValidEmail = false;

    String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(emailExpression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches())
    {
        isValidEmail = true;
    }
    return isValidEmail;
}

#5楼

For regex lovers, the very best (eg consistant with RFC 822) email's pattern I ever found since now is the following (before PHP supplied filters). 对于正则表达式爱好者来说,自从现在以来我发现最好的电子邮件模式(例如,与RFC 822一致)如下(在PHP提供的过滤器之前)。 I guess it's easy to translate this into Java - for those playing with API < 8 : 我想将其转换为Java很容易-对于使用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 ;
}

#6楼

Call This Method where you want to validate email ID. 在要验证电子邮件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;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值