I am trying to validate an email in the edittext. Below are my codes used to check email pattern. My app is returning an error message even though the email entered is of valid format. I can't seem to find the problem.
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();
}
});
}