赞
踩
本文翻译自: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?
参考:https://stackoom.com/question/7dF0/我应该如何验证电子邮件地址
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兼容。
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();
-
- }
- 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;
- }
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 ;
- }
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;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。