在Android中验证电子邮件地址(例如从用户输入字段)的好技术是什么?emailvalidator似乎不可用。还有其他库做这个,包括在Android已经或我必须使用RegExp?


当前回答

使用android:inputType="textEmailAddress"如下:

       <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:inputType="textEmailAddress"
        android:id="@+id/email"
        />

and:

       boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
                .matches();
      }

其他回答

最简单的电子邮件验证方式。

EditText TF;
public Button checkButton;

public final Pattern EMAIL_ADDRESS_PATTERN = 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}" +
          ")+"
      );
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

   TF=(EditText) findViewById(R.id.TF);
   checkButton=(Button) findViewById(R.id.checkButton);

    checkButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
           String email=TF.getText().toString();
           if(checkEmail(email))
              Toast.makeText(getApplicationContext(),"Valid Email Addresss", Toast.LENGTH_SHORT).show();
           else
              Toast.makeText(getApplicationContext(),"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
    }
    });
}
private boolean checkEmail(String email) {
    return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}}

对于正则表达式爱好者来说,我迄今为止发现的最好的(例如与RFC 822一致)电子邮件模式如下(在PHP提供过滤器之前)。我想对于那些使用API < 8的人来说,将其翻译成Java很容易:

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 ;
}
    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;
}

可以使用正则表达式。大致如下。

Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
String email = "xyz@xyzdomain.com";
Matcher matcher = pattern.matcher(email);
boolean matchFound = matcher.matches();

注意:检查上面给出的正则表达式,不要按原样使用它。

使用android:inputType="textEmailAddress"如下:

       <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email"
        android:inputType="textEmailAddress"
        android:id="@+id/email"
        />

and:

       boolean isEmailValid(CharSequence email) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email)
                .matches();
      }