当前位置:   article > 正文

我应该如何验证电子邮件地址?_apache commons validator 验证邮箱

apache commons validator 验证邮箱

本文翻译自: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:

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

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

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

  1. public static final Pattern EMAIL_ADDRESS
  2. = Pattern.compile(
  3. "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
  4. "\\@" +
  5. "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
  6. "(" +
  7. "\\." +
  8. "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
  9. ")+"
  10. );

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. 电子邮件是您的电子邮件。

  1. public boolean validateEmail(String email) {
  2. Pattern pattern;
  3. Matcher matcher;
  4. String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  5. pattern = Pattern.compile(EMAIL_PATTERN);
  6. matcher = pattern.matcher(email);
  7. return matcher.matches();
  8. }

#4楼

  1. public boolean isValidEmail(String email)
  2. {
  3. boolean isValidEmail = false;
  4. String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
  5. CharSequence inputStr = email;
  6. Pattern pattern = Pattern.compile(emailExpression, Pattern.CASE_INSENSITIVE);
  7. Matcher matcher = pattern.matcher(inputStr);
  8. if (matcher.matches())
  9. {
  10. isValidEmail = true;
  11. }
  12. return isValidEmail;
  13. }

#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的用户:

  1. private static function email_regex_pattern() {
  2. // Source: http://www.iamcal.com/publish/articles/php/parsing_email
  3. $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  4. $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  5. $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
  6. '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  7. $quoted_pair = '\\x5c[\\x00-\\x7f]';
  8. $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
  9. $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
  10. $domain_ref = $atom;
  11. $sub_domain = "($domain_ref|$domain_literal)";
  12. $word = "($atom|$quoted_string)";
  13. $domain = "$sub_domain(\\x2e$sub_domain)*";
  14. $local_part = "$word(\\x2e$word)*";
  15. $pattern = "!^$local_part\\x40$domain$!";
  16. return $pattern ;
  17. }

#6楼

Call This Method where you want to validate email ID. 在要验证电子邮件ID的位置调用此方法。

  1. public static boolean isValid(String email)
  2. {
  3. String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
  4. CharSequence inputStr = email;
  5. Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
  6. Matcher matcher = pattern.matcher(inputStr);
  7. if (matcher.matches())
  8. {
  9. return true;
  10. }
  11. else{
  12. return false;
  13. }
  14. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/196211
推荐阅读
相关标签
  

闽ICP备14008679号