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


当前回答

根据模式。EMAIL_ADDRESS,这个邮件是正确的“abc@abc.c”。所以我在模式中修改了正则表达式。EMAIL_ADDRESS并增加了域的最小长度。 下面是Kotlin的函数:

fun isEmailValid(email: String): Boolean =
    email.isNotEmpty() && 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\\-]{1,25}" +
                ")+"
    ).matcher(email).matches()

我只是将域部分从{0,25}改为{1,25}。

其他回答

Kotlin扩展函数

fun EditText.isValidEmail() : Boolean{
    return if(Pattern
            .compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")
            .matcher(text.toString()).matches()){
        true
    }else {
        hint = context.getString(R.string.invalid_email_adress)
        false
    }
}

Use

if(!emailEt.isValidEmail()){
    return
}

你可以通过oval.jar文件在android中进行任何类型的验证。OVal是一种实用的、可扩展的通用验证框架,适用于任何类型的Java对象。

点击这个链接:http://oval.sourceforge.net/userguide.html

你可以从这里下载:http://oval.sourceforge.net/userguide.html#download

您可以通过在变量中设置标记来使用验证

public class Something{

    @NotEmpty  //not empty validation
    @Email     //email validation
    @SerializedName("emailAddress")
    private String emailAddress;
}

   private void checkValidation() {
        Something forgotpass.setEmailAddress(LoginActivity.this.dialog_email.getText().toString());
        Validator validator = new Validator();
        //collect the constraint violations
        List<ConstraintViolation> violations = validator.validate(forgotpass);
        if(violations.size()>0){
            for (ConstraintViolation cv : violations){
                if(cv.getMessage().contains("emailAddress")){
                    dialog_email.setError(ValidationMessage.formattedError(cv.getMessage(), forgotpass));
                }
            }
        }
}

对于正则表达式爱好者来说,我迄今为止发现的最好的(例如与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 ;
}

如果您正在使用API 8或更高版本,您可以使用现成的Patterns类来验证电子邮件。示例代码:

public final static boolean isValidEmail(CharSequence target) {
    if (target == null) 
        return false;

    return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

如果您支持的API级别低于8,那么您可以简单地将Patterns.java文件复制到您的项目中并引用它。您可以从这个链接获得Patterns.java的源代码

试试这个简单的方法,它不能接受以数字开头的电子邮件地址:

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