0

I am trying to add a feedback form on my Android app where I need to test the email textedit field but it not work properly. Its always show me the message "Please enter a valid email address" id I put a valid address.

if (!isEmailValid(emailValid)){
    String estring = "Please enter a valid email address";
    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
    txtEmail.requestFocus();
    txtEmail.setError(ssbuilder);
}


public static boolean isEmailValid(String email) {
    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

enter image description here Bellow is my full code.

public class ContactFeedback extends AppCompatActivity {
    private Button mBtnFeedback;
    int ecolor = R.color.red;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_form_ui);

        //layout fields
        final EditText txtName = findViewById(R.id.feedbackName);
        final EditText txtEmail = findViewById(R.id.feedbackEmail);
        final EditText txtMobile = findViewById(R.id.feedbackPhone);
        final EditText txtMessage = findViewById(R.id.feedbackMessage);
        final Button mBtnFeedback = findViewById(R.id.feedbackSubmit);
        final String emailValid = txtEmail.getText().toString().trim();

        mBtnFeedback.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(txtName.getText().toString().trim().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Enter Your name", Toast.LENGTH_SHORT).show();
                    String estring = "Enter Your name";
                    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    txtName.requestFocus();
                    txtName.setError(ssbuilder);
                }
                else if(txtEmail.getText().toString().trim().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Enter Your email", Toast.LENGTH_SHORT).show();
                    String estring = "Please enter a valid email address";
                    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    txtEmail.requestFocus();
                    txtEmail.setError(ssbuilder);
                }

                else if (!isEmailValid(emailValid)){
                    String estring = "Please enter a valid email address";
                    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    txtEmail.requestFocus();
                    txtEmail.setError(ssbuilder);
                }

                else if(txtMobile.getText().toString().trim().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Enter Your Mobile", Toast.LENGTH_SHORT).show();
                    String estring = "Please enter a valid Mobile Number";
                    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    txtMobile.requestFocus();
                    txtMobile.setError(ssbuilder);
                }
                else if(txtMessage.getText().toString().trim().equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Enter Your Message", Toast.LENGTH_SHORT).show();
                    String estring = "Please enter Message";
                    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
                    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
                    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
                    txtMessage.requestFocus();
                    txtMessage.setError(ssbuilder);
                }

                else {
                    String name = txtName.getText().toString();
                    String email = txtEmail.getText().toString();
                    String message = txtMessage.getText().toString();
                    String phone = txtMobile.getText().toString();

                    String to = "example@myemail.com";
                    String subject = "Contact Feedback ";

                    Intent mEmail = new Intent(Intent.ACTION_SEND);
                    mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{to});
                    mEmail.putExtra(Intent.EXTRA_SUBJECT, subject);
                    mEmail.putExtra(Intent.EXTRA_TEXT, "Name: " + name + "<br />Mobile: " + phone + "<br />Email: " + email + "<br />Message: " + message);

                    // prompts to choose email client
                    mEmail.setType("message/rfc822");

                    startActivity(Intent.createChooser(mEmail, "Choose an email client to send your feedback!"));
                }

            }
        });


    }
    public static boolean isEmailValid(String email) {
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Firefog
  • 3,094
  • 7
  • 48
  • 86

6 Answers6

1

There is a mistake, you are getting email from edittext in onCreate() method.In onCreate() method your email edittext is blank. Please move below line to onClick method of button, It will work:

final String emailValid = txtEmail.getText().toString().trim();
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
1

Declare globally

 String emailValid = "";

and finally

final String emailValid = txtEmail.getText().toString().trim();

Move this line inside,

mBtnFeedback.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
emailValid = txtEmail.getText().toString().trim();
..
}

This will make sure that emailValid gets the string on each button click.

sanjeev
  • 1,664
  • 19
  • 35
0

Here is how to validate the email

public static boolean isValidEmail(CharSequence target) {
    return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}

If you are using Regexp's then below is used.

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
0

The problem seems to be the if-else branching you are using. You are using else if in each block. So at run-time only one block of code will execute. To fix this, instead using else if use if. As an example:

if(txtName.getText().toString().trim().equals(""))
{
    Toast.makeText(getApplicationContext(), "Enter Your name", Toast.LENGTH_SHORT).show();
    String estring = "Enter Your name";
    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
    txtName.requestFocus();
    txtName.setError(ssbuilder);
}
if(txtEmail.getText().toString().trim().equals(""))
{
    Toast.makeText(getApplicationContext(), "Enter Your email", Toast.LENGTH_SHORT).show();
    String estring = "Please enter a valid email address";
    @SuppressLint("ResourceAsColor") ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
    ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
    txtEmail.requestFocus();
    txtEmail.setError(ssbuilder);
}

// More codes...
salmanwahed
  • 9,450
  • 7
  • 32
  • 55
0

Use this method for validating email address.it's returns true if the email address is correct otherwise false.

 public boolean emailValidation(String email) {
        final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
Ronak Ukani
  • 603
  • 5
  • 20
0

To check and validate the string as Email Pattern Use predefined Pattern Utility by Android itself.

https://developer.android.com/reference/android/util/Patterns.html?hl=en#EMAIL_ADDRESS

Code snippet for using the same.

  EditText email = view.findViewById(R.id.email);
     String eEmailString = email.getText().toString().trim();
        if (!Patterns.EMAIL_ADDRESS.matcher(eEmailString).matches()) {
                 // If email doesn't matches email regex
                }