Domanda

i want to match the email and confirm_email if email and confirm email doesn't match then it has to give an error . When both matches then it must not display as error but what ever i enter there it is going to "Confirm Email is Not Matching" in the code below can anyone tell how to make an validation for email and confirm email checking

    public class MainActivity extends Activity {
    String valid_email = null;
    String v5,v4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText editText3 = (EditText)findViewById(R.id.email);
    final EditText editText4 = (EditText)findViewById(R.id.confirm_email);

    editText3.addTextChangedListener(new TextWatcher() {    
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {  
            Is_Valid_Email_Address(editText3);
        }
    });
    editText4.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            }

        @Override
        public void afterTextChanged(Editable s) {

            Is_Valid_Confirm_Address(editText4);
        }
    });

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            v4 = editText3.getText().toString();
            v5 = editText4.getText().toString();  

public void Is_Valid_Confirm_Address(EditText editText4) {
    if (editText4.getText().toString() == null) {
        editText4.setError("Invalid Email Address");
        valid_email = null;
    } else if (isEmailValid(editText4.getText().toString()) == false) {
        editText4.setError("Invalid Email Address");
        valid_email = null;
    } else if (editText4.getText().toString() != v4) {
        editText4.setError("Confirm Email is Not Matching"); // if i enter the same address it will give this error
        valid_email = null;
    } else {
        valid_email = editText4.getText().toString();
    }

}

public void Is_Valid_Email_Address(EditText editText3) {
    if (editText3.getText().toString() == null) {
        editText3.setError("Invalid Email Address");
        valid_email = null;
    } else if (isEmailValid(editText3.getText().toString()) == false) {
        editText3.setError("Invalid Email Address");
        valid_email = null;
    } else {
        valid_email = editText3.getText().toString();
    }

}

private boolean isEmailValid(String email) {

    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}
È stato utile?

Soluzione

Try this way

 if (editText3.getText().toString().equals(editText4.getText().toString())) {

    editText4.setError("Invalid Email Address");

 }else{

  editText4.setError("Email Address Match!!!!");
  }

Used .equals() method for String comparison

Altri suggerimenti

And you should .equals() instead of == when comparing Strings

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top