【发布时间】:2014-05-15 00:53:32
【问题描述】:
我正在尝试验证编辑文本中的电子邮件。以下是我用于检查电子邮件模式的代码。即使输入的电子邮件格式有效,我的应用程序仍会返回错误消息。我似乎找不到问题。
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();
}
});
}
【问题讨论】:
-
请记录错误日志....
-
没有错误是错误日志。错误消息来自 setError 响应。
标签: android validation email