【问题标题】:How to check valid email format entered in edittext如何检查在edittext中输入的有效电子邮件格式
【发布时间】:2013-05-24 14:30:07
【问题描述】:

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

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" />

【问题讨论】:

标签: android android-edittext


【解决方案1】:

您可以使用正则表达式 (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
}

【讨论】:

  • 嘿,我还有一个查询,我还想检查日期的验证,因为有人告诉我该怎么做
  • 日期的检查类型
  • 知道了,哥们你能告诉我如何只检查文本的验证,比如名字
【解决方案2】:

在其中传递电子邮件 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;
}

【讨论】:

    【解决方案3】:

    将 EditText 传递给此方法,如果电子邮件地址有效,则返回 true,否则返回 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 ;
    }
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:
        **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)
            });
        
        }
        

        }

        【讨论】:

          【解决方案6】:

          参考这个how to check edittext's text is email address or not?

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

          在 Android 2.2+ 上使用这个:

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

          【讨论】:

            【解决方案7】:

            Following this article

            Method-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");
            
                               }
            

            【讨论】:

              【解决方案8】:

              你也可以使用吹气:

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

              【讨论】:

                【解决方案9】:
                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

                【讨论】:

                • 感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将有助于未来的读者更好地了解正在发生的事情,尤其是那些刚接触该语言并难以理解概念的社区成员。
                猜你喜欢
                • 2014-02-02
                • 2017-01-14
                • 1970-01-01
                • 2016-07-23
                • 2017-06-06
                • 2015-08-07
                • 2016-01-03
                • 1970-01-01
                • 2016-08-23
                相关资源
                最近更新 更多