2016-07-19 70 views
-5

僅當Android中的EditText不爲空時驗證電子郵件格式。如果該字段爲空,則驗證不應被檢查。在這種情況下,我沒有找到任何對我有用的解決方案。僅當字段在Android中不爲空時才進行電子郵件驗證

+1

我可以幫忙,但首先在這裏顯示你的努力,因爲你已經做了 – Vickyexpert

+0

你到目前爲止嘗試過什麼?首先搜索谷歌 –

回答

1

試試這個代碼

final EditText emailEditTxt= (EditText)findViewById(R.id.text); 

String emailStr = emailEditTxt.getText().toString().trim(); 

if(emailStr!=null) 

if(emailStr.length()>=1){ 

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


if (emailStr .matches(emailPattern)) 
{ 
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show(); 
} 
} 
0

使用此方法檢查電子郵件模式是否有效

public static boolean isEmailValid(CharSequence email) { 
     return Patterns.EMAIL_ADDRESS.matcher(email).matches(); 
    } 
0

validateEmail

private boolean validateEmail() { 
if (!edemail.getText().toString().trim().isEmpty()&&!ValidationMethod.emailValidation(edemail.getText().toString())) 
    { 

     input_layout_email.setError("Invalid Email"); // Your textInput Layout or other set your error on edittext email 
     requestFocus(edemail); 
     return false; 
    } 

    else { 
     input_layout_email.setErrorEnabled(false); 
    } 

    return true; 
} 

    // if you need requestfocus other wise remove 
private void requestFocus(View view) { 
    if (view.requestFocus()) { 
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    } 
} 

ValidationMethod.java //把你的項目

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class ValidationMethod { 

    static Matcher m; 
    static String emailExpression = "^[\\w\\.-][email protected]([\\w\\-]+\\.)+[A-Z]{2,20}$"; 
    static Pattern emailPattern = Pattern.compile(emailExpression, Pattern.CASE_INSENSITIVE); 
    static String passwordExpression ="((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\\W).{8,20})"; 
    static Pattern passwordPattern=Pattern.compile(passwordExpression); 

    public static boolean emailValidation(String s) 
    { 
     if(s == null) 
     { 
      return false; 
     } 
     else 
     { 
      m = emailPattern.matcher(s); 
      return m.matches(); 
     } 
    } 

    public static boolean passwordValidation(String s) 
    { 
     if(s == null) 
     { 
      return false; 
     } 
     else 
     { 
      m = passwordPattern.matcher(s); 
      return m.matches(); 
     } 
    } 

    public static boolean emailValidation2(String s) 
    { 
     m = emailPattern.matcher(s); 
     return m.matches(); 
    } 
} 
1

試試這個..

,並使用flag值進一步使用

0

嘗試這

<form> 
    <lable for="userEmail">Email : </label> 
    <input type="email" id="userEmail" placeholder="[email protected]" required="required"> 
<form> 
0

可以說你已經像這樣聲明瞭EditText。

EditText emailField;

那麼在你的方法

emailField = (EditText)view.getViewById(R.id.NAME_OF_FIELD_IN_XML); 
if(!TextUtils.isBlank(emailField.getText().toString())){ 
    //validate your email address 
} 

使用下面的鏈接,電子郵件驗證碼。

http://stackoverflow.com/questions/12947620/email-address-validation-in-android-on-edittext 
0

您可以嘗試此驗證。

public boolean Email_validation(String CorpId) 
{ 
    String regExpn = 
      "^(([\\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})$"; 

    CharSequence inputStr = CorpId; 

    Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE); 
    Matcher matcher = pattern.matcher(inputStr); 

    if(matcher.matches()) 
     return true; 
    else 
     return false; 
} 
相關問題