仅当Android中的字段不为空时才进行电子邮件验证

时间:2016-07-19 05:51:11

标签: java android android-edittext email-validation

仅当Android中的EditText不为空时才验证电子邮件格式。如果该字段为空白,则不应检查验证。我在这个场景中没有找到任何对我有用的解决方案。

7 个答案:

答案 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._-]+@[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();
}
}

答案 1 :(得分:1)

试试这个..

boolean flag;
String pass = editText.getText().toString().trim();
if(!pass.equals("")) {
    flag = isEmailValid(pass);
}

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

并使用flag值进一步使用

答案 2 :(得分:0)

使用此方法检查电子邮件模式是否有效

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

答案 3 :(得分: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\\.-]+@([\\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();
    }
}

答案 4 :(得分:0)

试试这个

<form>
    <lable for="userEmail">Email : </label>
    <input type="email" id="userEmail" placeholder="johndoe@example.com" required="required">
<form>

答案 5 :(得分: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

答案 6 :(得分: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;
}