繁体   English   中英

如何检查在edittext中输入的有效电子邮件格式

[英]How to check valid email format entered in edittext

我创建了一个登录注册表单,我想在其中编辑文本以插入电子邮件地址我使用过输入类型文本电子邮件地址把它不检查天气它是一个有效的电子邮件格式或不知道如何在 android 中检查电子邮件格式提前致谢

enter code here<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10"
    android:inputType="textEmailAddress" />

请参阅此如何检查edittext 的文本是否为电子邮件地址?

我引用了最打勾的答案,我认为这是最优雅的。

在 Android 2.2+ 上使用这个:

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

您可以使用正则表达式 (Regex) 来检查电子邮件模式。

Pattern pattern1 = Pattern.compile( "^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+");

Matcher matcher1 = pattern1.matcher(Email);

if (!matcher1.matches()) {
    //show your message if not matches with email pattern
}
**Please follow the following Steps**

    Seet - 1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/textView_email"
        android:layout_marginTop="40dp"
        android:hint="Email Adderess"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textView_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Email Validation Example" />

</RelativeLayout>
 Seet - 2 import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText;
 Seet - 3

公共类 MainActivity 扩展 Activity {

private EditText email;

private String valid_email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initilizeUI();
}

/**
 * This method is used to initialize UI Components
 */
private void initilizeUI() {
    // TODO Auto-generated method stub

    email = (EditText) findViewById(R.id.editText_email);

    email.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub
            Is_Valid_Email(email); // pass your EditText Obj here.
        }

        public void Is_Valid_Email(EditText edt) {
            if (edt.getText().toString() == null) {
                edt.setError("Invalid Email Address");
                valid_email = null;
            } else if (isEmailValid(edt.getText().toString()) == false) {
                edt.setError("Invalid Email Address");
                valid_email = null;
            } else {
                valid_email = edt.getText().toString();
            }
        }

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

}

}

跟着这篇文章

方法 1) 以下适用于 android 2.2 以上

    public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

方法 2) 使用正则表达式并将验证添加到 EditText 的 textChangeListener 中:

 EdiText emailValidate;
String email = emailValidate.getEditableText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

emailValidate .addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable s) { 

    if (email.matches(emailPattern) && s.length() > 0)
        { 
            Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
            // or
            textView.setText("valid email");
        }
        else
        {
             Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
            //or
            textView.setText("invalid email");
        }
    } 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // other stuffs 
    } 
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    // other stuffs 
    } 
}); 

方法 3

    public static boolean isEmailValid(String email) {
    boolean isValid = false;

    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()) {
        isValid = true;
    }
    return isValid;
}

方法四

if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {

                       edttextEmail.setError("Invalid Email Address");

                   }

在其中传递电子邮件 ID:-

      public static boolean emailAddressValidator(String emailId) {
    Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*" + "\\@"
            + "\\w+([-.]\\w+)*" + "\\." + "\\w+([-.]\\w+)*");

    Matcher matcher = pattern.matcher(emailId);
    if (matcher.matches())
        return true;
    else
        return false;
}

通行证的EditText这种方法,这将返回true ,如果E-mail地址是有效的否则它会返回false

/**
 * method is used for checking valid email id format.
 * 
 * @param email
 * @return boolean true for valid false for invalid
 */
public static boolean isEmailAddressValid(String email) {
    boolean isEmailValid = false;

    String strExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern objPattern = Pattern.compile(strExpression , Pattern.CASE_INSENSITIVE);
    Matcher objMatcher = objPattern .matcher(inputStr);
    if (objMatcher .matches()) {
        isEmailValid = true;
    }
    return isEmailValid ;
}

这是一个非常好的 Android 表单编辑文本link ,它是 EditText 的扩展,它为编辑文本带来了数据验证功能。

它为编辑文本值(例如电子邮件、号码、电话、信用卡等)提供自定义验证,这可能对您有用..

您也可以使用打击表达:

^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
private TextInputLayout textInputPassword;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textInputEmail = findViewById(R.id.text_input_email);
   
   
}
private boolean validateEmail() {
    String emailInput = textInputEmail.getEditText().getText().toString().trim();
    if (emailInput.isEmpty()) {
        textInputEmail.setError("Field can't be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
        textInputEmail.setError("Please enter a valid email address");
        return false;
    } else {
        textInputEmail.setError(null);
        return true;
    }
}

你可以在这里查看详细的解决方案: https : //codinginflow.com/tutorials/android/validate-email-password-regular-expressions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM