2012-10-18 61 views

回答

46

要執行電子郵件驗證我們有很多方法,但簡單的&最簡單的方法是兩種方法

1-使用EditText(....).addTextChangedListener其一直在EditText box即EMAIL_ID上的每個輸入觸發是無效或有效使用if-else條件

/** 
* Email Validation ex:- [email protected] 
*/ 


final EditText emailValidate = (EditText)findViewById(R.id.textMessage); 

final TextView textView = (TextView)findViewById(R.id.text); 

String email = emailValidate.getText().toString().trim(); 

String emailPattern = "[a-zA-Z0-9._-][email protected][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 
    } 
}); 

2-簡單的方法。使用getText()獲取EditText框字符串,並與爲電子郵件提供的模式進行比較。如果模式不匹配或macthes,onClick按鈕烤麪包消息。它不會觸發EditText框中每個字符的輸入。簡單的例子如下所示。

final EditText emailValidate = (EditText)findViewById(R.id.textMessage); 

final TextView textView = (TextView)findViewById(R.id.text); 

String email = emailValidate.getText().toString().trim(); 

String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

// onClick of button perform this simplest code. 
if (email.matches(emailPattern)) 
{ 
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show(); 
} 
+1

Thanx,它幫了我很多 –

+1

第二個簡單而好...非常感謝你 – Srihari

+0

這不是一個有效的電子郵件模式。 [email protected]失敗。 –

23

使用此方法驗證您的電子郵件格式。將電子郵件作爲字符串傳遞,如果格式正確則返回true,否則返回false。

/** 
* validate your email address format. [email protected] 
*/ 
public boolean emailValidator(String email) 
{ 
    Pattern pattern; 
    Matcher matcher; 
    final 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

感謝您的建議.. –

6

試試這個:

if (!emailRegistration.matches("[a-zA-Z0-9._-][email protected][a-z]+.[a-z]+")) { 
    editTextEmail.setError("Invalid Email Address"); 
} 
+0

感謝您的建議.. –

+0

歡迎您:) –

+2

這不適用於域名中有多個點的電子郵件,例如@ woof。 co.za。此外,它匹配沒有點像名稱@ gmailcom – Matiaan

3

試試這個

public static final 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}" + 
       ")+" 
     ); 

和TNE編輯文本

final String emailText = email.getText().toString(); 
EMAIL_ADDRESS_PATTERN.matcher(emailText).matches() 
+0

感謝您的建議。 –

+0

這是Android模式,並接受[email protected]。你有關於這個場景的任何解決方案 – theLazyFinder

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

    String expression = "^[\\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; 
} 
+0

感謝您的建議.. –

+0

這工作完美的我,可能是表達使差異.. –

3

這是一個示例我thod我創建驗證電子郵件地址,如果傳遞的字符串參數是一個有效的電子郵件地址,它返回true,否則返回false。

private boolean validateEmailAddress(String emailAddress){ 
    String expression="^[\\w\\-]([\\.\\w])+[\\w][email protected]([\\w\\-]+\\.)+[A-Z]{2,4}$"; 
     CharSequence inputStr = emailAddress; 
     Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE); 
     Matcher matcher = pattern.matcher(inputStr); 
     return matcher.matches(); 
} 
+0

謝謝您的建議.. –

+0

@TechEnd歡迎 – AppMobiGurmeet

22

這太容易了:

添加此方法電子郵件地址檢查是否有效

private boolean isValidEmaillId(String email){ 

    return Pattern.compile("^(([\\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})$").matcher(email).matches(); 
    } 

現在用字符串檢查EditText上 的:

if(isValidEmaillId(edtEmailId.getText().toString().trim())){ 
    Toast.makeText(getApplicationContext(), "Valid Email Address.", Toast.LENGTH_SHORT).show(); 
}else{  
    Toast.makeText(getApplicationContext(), "InValid Email Address.", Toast.LENGTH_SHORT).show(); 
} 

完成

447
public final static boolean isValidEmail(CharSequence target) { 
    return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches()); 
} 

編輯::它將工作在Android 2.2+起! 編輯:添加缺少;

+53

+1使用內置功能。我也會用'TextUtils.isEmpty(target)'替換'target == null'。 – rciovati

+0

此功能doesn的驗證,如果我使用滑動選項,而輸入任何替代請嗎? – Anitha

+15

單行解決方案'return!TextUtils.isEmpty(target)&& Patterns.EMAIL_ADDRESS.matcher(target).matches();' –

5

使用此方法來驗證EMAIL: -

public static boolean isEditTextContainEmail(EditText argEditText) { 

      try { 
       Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); 
       Matcher matcher = pattern.matcher(argEditText.getText()); 
       return matcher.matches(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return false; 
      } 
     } 

讓我知道如果您有任何疑問?