即使电子邮件有效,电子邮件检查也会显示错误消

时间:2014-04-03 09:34:06

标签: android validation email

我正在尝试在edittext中验证电子邮件。以下是用于检查电子邮件模式的代码。即使输入的电子邮件格式有效,我的应用程序也会返回错误消息。我似乎无法找到问题。

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

private boolean checkEmail(String email) {
    return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post_item_form);

    // Edit Text
    itemName = (EditText) findViewById(R.id.i_itemname);
    itemPrice = (EditText) findViewById(R.id.i_price);
    itemDesc = (EditText) findViewById(R.id.i_des);
    inputEmail = (EditText) findViewById(R.id.i_email);
    contact = (EditText) findViewById(R.id.i_contact);
    password = (EditText) findViewById(R.id.i_password);
    itemCat = (Spinner) findViewById(R.id.spinner1);


    category = itemCat.getSelectedItem().toString();
    postemail = inputEmail.getText().toString();

    // Create button
    Button btnCreateProduct = (Button) findViewById(R.id.submitpostitem);

    // button click event
    btnCreateProduct.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            category = itemCat.getSelectedItem().toString();
            if (itemName.length() < 2) {
                itemName.setError("Invalid item name");
                return;
            } else if (itemPrice.length() == 0) {
                itemPrice.setError("Invalid item price");
                return;
            } else if (!checkEmail(postemail)) {
                inputEmail.setError("Invalid email");
                return;
            } else if (contact.length() < 10 || contact.length() > 12) {
                contact.setError("Invalid contact");
                return;
            } else if (password.length() < 6) {
                password.setError("At least 6 characters long");
                return;
            } else if (category.equals("---")) {
                Toast.makeText(PostItemForm.this,
                        "Please choose a category", Toast.LENGTH_LONG)
                        .show();
                return;
            }

            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}

3 个答案:

答案 0 :(得分:1)

尝试使用以下代码验证电子邮件地址:

android.util.Patterns.EMAIL_ADDRESS.matcher(YourString).matches();

所以checkEmail必须如下:

private boolean checkEmail(String email) {
    if (TextUtils.isEmpty(email)) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }
}

您可以看到以下链接:

Email Address Validation in Android on EditText

How should I validate an e-mail address on Android?

答案 1 :(得分:1)

试试如下:

     Pattern m_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}" + ")+");

private boolean checkEmail(String email) {
       Matcher m_matcher = m_pattern.matcher(email.toString().trim());
       if (!m_matcher.matches()) 
            return true;
        else
       return false;
}

获取EditText点击监听器内的Button的值不在onCreate中。正如您在外部点击监听器上编写了行postemail = inputEmail.getText().toString();,它将始终获得之前的值。

只需在点击监听器中更改以下代码即可。

  btnCreateProduct.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        category = itemCat.getSelectedItem().toString();
        postemail = inputEmail.getText().toString();

     if (itemName.length() < 2) {
            itemName.setError("Invalid item name");
            return;
        } else if (itemPrice.length() == 0) {
            itemPrice.setError("Invalid item price");
            return;
        } else if (!checkEmail(postemail)) {
            inputEmail.setError("Invalid email");
            return;
        } else if (contact.length() < 10 || contact.length() > 12) {
            contact.setError("Invalid contact");
            return;
        } else if (password.length() < 6) {
            password.setError("At least 6 characters long");
            return;
        } else if (category.equals("---")) {
            Toast.makeText(PostItemForm.this,
                    "Please choose a category", Toast.LENGTH_LONG)
                    .show();
            return;
        }

        // creating new product in background thread
        new CreateNewProduct().execute();
    }
});

答案 2 :(得分:1)

试试这个......

在Oncreate方法之前粘贴此代码

 public static boolean isValidEmail(String str) {
    boolean isValid = false;
    if (Build.VERSION.SDK_INT >= 8) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(str).matches();
    }
    String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}

并在edittext

之后检查此条件
     if (!isValidEmail( inputEmail.getText().toString().trim())) {
     inputEmail.setError("Invalid Email ID");