2011-05-25 60 views
91

如何查看edittext的文字是不是電子郵件地址或不使用javascript正則表達式? 這裏我用inputtype="textEmailAddress"這是工作,但沒有顯示錯誤信息。如何檢查edittext的文本是否爲電子郵件地址?

+2

得到Patterns.java的源代碼當然PointerNull的答案在這裏是合理的,現代的答案? – Fattie 2014-05-23 11:17:49

+1

@JoeBlow是的,我同意,但「提問者」可以改變約定的答案的時間有限。...... – giulio 2015-08-24 06:25:17

回答

189
/** 
* method is used for checking valid email id format. 
* 
* @param email 
* @return boolean true for valid false for invalid 
*/ 
public static boolean isEmailValid(String email) { 
    String expression = "^[\\w\\.-][email protected]([\\w\\-]+\\.)+[A-Z]{2,4}$"; 
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); 
    Matcher matcher = pattern.matcher(email); 
    return matcher.matches(); 
} 

在此函數中傳遞您的編輯文本字符串。

右電子郵件驗證你需要服務器端認證


注意現在有Android中內置的方法,請參見下面的答案。

+0

謝謝你Droid我已經做到了。 – 2011-05-25 06:10:39

+1

這是正常工作。 – 2012-01-27 13:16:07

+7

'String'實現'CharSequence',所以我認爲從'email'到'inputStr'的轉換是多餘的。 – yoshi 2012-08-02 15:11:43

0

用於電子郵件驗證試試這個。

public boolean checkemail(String email) 
{ 

    Pattern pattern = Pattern.compile("[email protected]+\\.[a-z]+"); 
    Matcher matcher = pattern.matcher(email); 
    return matcher.matches(); 

} 
+0

sir請將您對[email protected]+*&.com的驗證作爲電子郵件 – 2012-01-27 13:15:21

322

在Android 2.2+使用:

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

真棒!真的有幫助 – 2017-09-01 09:27:02

12

我寫了一個擴展的EditText一個庫,它本身支持一些驗證方法,實際上是非常靈活的。

目前,我寫,本身支持(通過XML屬性)驗證方法是:

  1. 正則表達式:自定義正則表達式
  2. 數字:一個只有數字領域
  3. alpha:僅針對alpha字段
  4. alpha數字:猜怎麼着?
  5. 電子郵件:檢查該字段是一個有效的電子郵件
  6. 的信用卡:檢查該字段包含使用盧恩算法
  7. 電話有效的信用卡:檢查該字段包含一個有效的電話號碼
  8. 則domainName:檢查該字段包含一個有效的域名(總是通過在API級別測試< 8)
  9. ip地址:檢查該字段包含一個有效的IP地址 WEBURL:檢查該字段包含一個有效的URL(始終傳遞至API等級< 8測試)
  10. NOCHECK:它不檢查任何東西。(默認)

您可以點擊此處查看:https://github.com/vekexasia/android-form-edittext

希望你喜歡它:)

在我聯繫你就可以還找到電子郵件驗證一個例子頁面。我會在這裏複製的相對片段:

<com.andreabaccega.widget.FormEditText 
     style="@android:style/Widget.EditText" 
     whatever:test="email" 
     android:id="@+id/et_email" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint_email" 
     android:inputType="textEmailAddress" 
     /> 

還有一個測試應用程序,展示庫的可能性。

這是驗證電子郵件字段的應用程序的屏幕截圖。

email validation done thorugh xml+library

2

這裏的電子郵件是您的電子郵件ID。

public boolean validateEmail(String email) { 

    Pattern pattern; 
    Matcher matcher; 
    String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    pattern = Pattern.compile(EMAIL_PATTERN); 
    matcher = pattern.matcher(email); 
    return matcher.matches(); 

    } 
+0

當前電子郵件RFC的作者John Klensin有一個您拒絕的電子郵件地址,它包含一個+號 – arnt 2013-11-28 10:44:14

1

的簡單方法

private boolean isValidEmail(String email) 
{ 
    String emailRegex ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    if(email.matches(emailRegex)) 
    { 
     return true; 
    } 
    return false; 
} 
1

公共靜態布爾isEmailValid(字符串電子郵件){ 布爾的isValid = FALSE;

String expression = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@" 
      + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" 
      + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\." 
      + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" 
      + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" 
      + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$"; 
    // "^[\\w\\.-][email protected]([\\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; 
} 
1
I Hope this code is beneficial for you 

    public class Register extends Activity 
     { 
     EditText FirstName, PhoneNo, EmailId,weight; 
     Button Register; 
     private static final Pattern EMAIL_PATTERN = Pattern 
     .compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); 


    private static final Pattern USERFIRSTNAME_PATTERN = Pattern 
     .compile("[a-zA-Z0-9]{1,250}"); 
    private static final Pattern PHONE_PATTERN = Pattern 
     .compile("[a-zA-Z0-9]{1,250}"); 
     @Override 
    public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register); 


     Register=(Button) findViewById(R.id.register); 

     FirstName=(EditText)findViewById(R.id.person_firstname); 

      PhoneNo =(EditText)findViewById(R.id.phone_no); 
      EmailId=(EditText)findViewById(R.id.email_id); 
      weight=(EditText) findViewById(R.id.weight); 

     Register.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       sFirstName= FirstName.getText().toString(); 
       sPhoneNo= PhoneNo.getText().toString(); 
       sEmailId= EmailId.getText().toString(); 
       sweight= weight.getText().toString(); 

       if(sFirstName.equals("")||sPhoneNo.equals("")||sEmailId.equals("")||sweight.equals("")) 
       { 
        if ((!CheckUsername(sFirstName))) 
        { 
        Toast.makeText(Register.this, "FirstName can not be null",Toast.LENGTH_LONG).show(); 
        } 
        else if ((!Checkphoneno(sPhoneNo))) 
         { 
        Toast.makeText(Register.this, "ENTER VALID mobile no ",Toast.LENGTH_LONG).show(); 
         } 
        else if ((!CheckEmail(sEmailId))) 
         { 
         Toast.makeText(Register.this, "ENTER VALID EMAIL ID",Toast.LENGTH_LONG).show(); 
         } 
        else if ((!Checkweight(sweight))) 
         { 
        Toast.makeText(Register.this, "ENTER Weight in kg",Toast.LENGTH_LONG).show(); 
         } 
       } 

      } 
       private boolean CheckEmail(String sEmailId) { 

        return EMAIL_PATTERN.matcher(sEmailId).matches(); 
       } 



       private boolean CheckUsername(String sFirstName) { 

        return USERFIRSTNAME_PATTERN.matcher(sFirstName).matches(); 
       } 
       private boolean Checkphoneno(String sPhoneNo) { 

        return PHONE_PATTERN.matcher(sPhoneNo).matches(); 
       } 
       private boolean Checkweight(String sweight) { 

        return Weight_PATTERN.matcher(sweight).matches(); 
       } 


     }); 
+0

謝謝它有助於解決我的問題:) – 2015-03-25 10:39:12

+0

您的歡迎... – 2015-03-25 10:55:46

11

請遵循以下步驟

步驟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> 

步驟2:

import android.app.Activity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.widget.EditText; 

步驟3:

public class MainActivity extends 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) 
    }); 

} 

} 
2

以下代碼應該對您有用。

String email; 
check.setOnClickListener(new OnClickListener() { 


       public void onClick(View arg0) { 

        checkEmail(email); 
        if (checkMail) { 
         System.out.println("Valid mail Id"); 
        } 
       } 
      }); 

     } 
    } 

    public static boolean checkEmail(String email) { 

     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}" + ")+"); 
     return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); 
    } 
2

Apache Commons Validator可以像其他答案中提到的那樣使用。

步驟:1)從here

步驟下載jar文件:2)添加到您的項目庫

進口:

import org.apache.commons.validator.routines.EmailValidator; 

代碼:

String email = "[email protected]"; 
boolean valid = EmailValidator.getInstance().isValid(email); 

並允許本地地址::

boolean allowLocal = true; 
boolean valid = EmailValidator.getInstance(allowLocal).isValid(email); 
0

您可以通過正則表達式檢查

public boolean isValid(String strEmail) 
    { 
     pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); 
      matcher = pattern.matcher(strEmail); 

     if (strEmail.isEmpty()) { 
      return false; 
     } else if (!matcher.matches()) { 
      return false; 
     } 
     else 
     { 
     return true; 
     } 
    } 
0
private boolean isValidEmailID(String email) { 
    String PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
    Pattern pattern = Pattern.compile(PATTERN); 
    Matcher matcher = pattern.matcher(email); 
    return matcher.matches(); 
} 

For more validation click here

5

正如在回答中提到你可以使用如下的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文件複製到您的項目並引用它。你可以從this link

相關問題