Android电子邮件验证无效

时间:2015-05-13 11:09:41

标签: android eclipse mobile

在插入数据库之前验证电子邮件。但我无法验证。没有任何错误。

final EditText email=(EditText) findViewById(R.id.editText1);
final String Email1 = email.getText().toString().trim();
final String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+\\.+[a-z]+";
public void onClick(View v) {

            if((email.getText().toString().length()==0)) 
            {

                //email.setError( "Please Enter Valid Id" );
                Toast.makeText(LoginPage.this,"Please Enter EmailId", Toast.LENGTH_LONG)
                        .show();
                return;
               }
            else if(!(isValidEmail(Email1))){
                Toast.makeText(LoginPage.this,"Invalid Email", Toast.LENGTH_LONG)
                .show();
            return;
            }


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

4 个答案:

答案 0 :(得分:1)

您可以使用以下方法

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

让我详细解释一下,

        final EditText email=(EditText) findViewById(R.id.editText1);
        final String Email1 = email.getText().toString().trim();
    if(!isValidEmail(Email1 )){
    //INVALID EMAIL ID
    }else{
    //VALID EMAIL ID
    }

根据您的代码更新此

    final EditText email=(EditText) findViewById(R.id.editText1);
    final String Email1 = email.getText().toString().trim();

    public void onClick(View v) {

        if((Email1.toString().length()==0)) 
        {

            //email.setError( "Please Enter Valid Id" );
            Toast.makeText(LoginPage.this,"Please Enter EmailId", Toast.LENGTH_LONG)
                    .show();
            return;
           }
        else if(!isValidEmail(Email1)){
            Toast.makeText(LoginPage.this,"Invalid Email", Toast.LENGTH_LONG)
            .show();
        return;
        }
        }


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

答案 1 :(得分:1)

请尝试以下代码。

public static boolean emailValidation(String emailid) {
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(emailid);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }

答案 2 :(得分:0)

使用以下方法:

public static boolean isValidEmail(String email) {
        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 = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

<强>示例:

if((email.getText().toString().length()>0){
         if(isValidEmail(email.getText().toString())){
            //Valid email
         }
}

答案 3 :(得分:0)

public class Validation {
String pattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

public static boolean isValidEmail(EditText editText) {

        String email = editText.getText().toString().trim();
        if (email.matches(pattern)) {
            return true;
        } else {
            return false;
        }
    }
}

调用以上方法,您需要检查以下电子邮件的验证。 这对我有用

Validation.isValidEmail(yourEmail Edittext);