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


当前回答

你可以通过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));
                }
            }
        }
}

其他回答

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

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

在要验证电子邮件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;
   }
}

使用扩展函数的最简单的Kotlin解决方案:

fun String.isEmailValid() =
            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}" +
                            ")+"
            ).matcher(this).matches()

然后你可以这样验证:

"testemail6589@gmail.com".isEmailValid()

如果你在kotlin-multiplatform中没有访问Pattern,这是等价的:

fun String.isValidEmail() = Regex(emailRegexStr).matches(this)

不要使用reg-ex。

显然,下面是一个正确验证符合RFC 2822的大多数电子邮件地址的reg-ex,(对于“user@gmail.com.nospam”这样的地址仍然会失败,org.apache.commons.validator. emailvalidator也是如此)

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

验证电子邮件最简单的方法可能是向提供的地址发送确认电子邮件,如果它反弹,那么它是无效的。

如果你想执行一些基本的检查,你可以检查它的形式是*@*

如果你有一些业务逻辑特定的验证,那么你可以使用正则表达式来执行,例如必须是gmail.com帐户或其他东西。

我知道已经太迟了,但我还是会给你我的答案。

我使用这行代码来检查输入的电子邮件格式:

!TextUtils.isEmpty(getEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(getEmail).matches();

问题是,它只检查格式而不是拼写。

当我输入@gmal.com漏了I, @yaho.com又漏了一个o。 返回true。因为它满足电子邮件格式的条件。

我所做的是,我使用上面的代码。因为它会返回true如果 如果用户输入@gmail.com ONLY,开头没有文本。

格式检查

如果我输入这个电子邮件,它会给我:true,但拼写是错误的。在 我的textInputLayout错误

电子邮件地址@yahoo.com, @gmail.com, @outlook.com检查器

 //CHECK EMAIL
public boolean checkEmailValidity(AppCompatEditText emailFormat){

    String getEmail = emailFormat.getText().toString();
    boolean getEnd;

    //CHECK STARTING STRING IF THE USER
    //entered @gmail.com / @yahoo.com / @outlook.com only
    boolean getResult = !TextUtils.isEmpty(getEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(getEmail).matches();

    //CHECK THE EMAIL EXTENSION IF IT ENDS CORRECTLY
    if (getEmail.endsWith("@gmail.com") || getEmail.endsWith("@yahoo.com") || getEmail.endsWith("@outlook.com")){
        getEnd = true;
    }else {
        getEnd = false;
    }

    //TEST THE START AND END
    return (getResult && getEnd);
}

返回:假

返回:真

XML:

<android.support.v7.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editTextEmailAddress"
            android:inputType="textEmailAddress|textWebEmailAddress"
            android:cursorVisible="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:singleLine="true"
            android:maxLength="50"
            android:theme="@style/EditTextCustom"/>

注意:我试图从EditText中获取值,并使用split对它甚至StringTokenizer。都对我虚情假意。