Benz
Benz

Reputation: 237

Email check gives error message even though email is valid

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();
        }
    });
}

Upvotes: 0

Views: 935

Answers (3)

GrIsHu
GrIsHu

Reputation: 23648

Try out as below:

     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;
}

Get the value of the EditText inside the Button click listener not in onCreate. As you have written line postemail = inputEmail.getText().toString(); outside click listener it will be always get the previous value.

Just change your code as below in your click listener.

  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();
    }
});

Upvotes: 1

Amardeepvijay
Amardeepvijay

Reputation: 1628

try this....

paste this code before Oncreate method

 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;
}

and check this condition after edittext

     if (!isValidEmail( inputEmail.getText().toString().trim())) {
     inputEmail.setError("Invalid Email ID");

Upvotes: 1

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

try following code for validate Email address:

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

so checkEmail must be like:

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

you can see following link:

Email Address Validation in Android on EditText

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

Upvotes: 1

Related Questions