0

I am trying to compare two EditTexts when the user clicks a button. However the IF statement returns true even if the two strings are different.

    final EditText email= (EditText)findViewById(R.id.txtEmail);
    final EditText emailconf= (EditText)findViewById(R.id.txtEmailConf);
    final EditText password = (EditText)findViewById(R.id.txtPassword1);
    final EditText passconf = (EditText)findViewById(R.id.txtPasswordConf);

    final String emailInput = email.getText().toString();
    final String emailconfInput = emailconf.getText().toString();
    final String passinput = password.getText().toString();
    final String passconfinput = passconf.getText().toString();

    //When our register button is pressed
 btnRegister.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(emailInput.equalsIgnoreCase(emailconfInput)){
                if(passinput.equalsIgnoreCase(passconfinput)){
                    new RegisterTask().execute();
                }
                else{
                    displayDialogue("Error", "Your password's do not match, please try again", "Re-input details");

                }

            }else{
                displayDialogue("Error", "Your email's do not match, please try again", "Re-input details");

            }


        }

    });

If the two are correct then it will run an Asynctask (RegisterTask)

Any help would be great, thanks

user2960452
  • 41
  • 1
  • 6
  • 2
    Print to the console if the 2 strings that you are comparing are really different. – Kami Dec 18 '13 at 20:23
  • What do you mean by different? If they're the same except for case, that's expected when using `equalsIgnoreCase`. – thegrinner Dec 18 '13 at 20:26
  • For exmaple, if I write test@test.com in email but bob@bob.com in the confirm email it doesn't display an error. – user2960452 Dec 18 '13 at 20:30

4 Answers4

2
final String passinput = password.getText().toString();
final String passconfinput = passconf.getText().toString();

Move this part to the onClick() so that the text is pulled from the EditTexts only then. In onCreate() they're both empty and two empty strings will match.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Are you sure it is giving true in every case? Apart from equals method try compareTo method it will work fine

0

I recommend to you Sanitise both fields, because if the user puts an empty space at the beginning or end of the field, equalsIgnoreCase will return false.

Try methods like trim(), remove invalid chars, and then compare, it's up to what you want to check.

noni
  • 2,927
  • 19
  • 18
-5
if(emailInput == emailconfInput){

if yout change to that you shouldnt have a problem... oh and i wouldnt suggest ignoring case on password check.

Fts Dev
  • 1
  • 1