I have this activity in which I want to validate the confirm password field.This is my code-:
nt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(email.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("E-mail field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(pass.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Password field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(conpass.getText().toString()!= pass.getText().toString() ){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Passwords do not match");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(name.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Name field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(dob.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Date of birth field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(address.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Address field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(city.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("City field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(zip.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Zip field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(phone.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Phone No. field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else if(mobile.getText().toString().equals("")){
AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("Mobile No field is empty");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
else{
String mail = email.getText().toString();
String pas = pass.getText().toString();
String fname = name.getText().toString();
String dateob = dob.getText().toString();
String add12 = address.getText().toString();
String tow = city.getText().toString();
String zip1 = zip.getText().toString();
String mob = mobile.getText().toString();
String phn = phone.getText().toString();
Intent per = new Intent(getApplicationContext(), Register2.class);
per.putExtra("email", mail);
per.putExtra("name", fname);
per.putExtra("password", pas);
per.putExtra("mobile", mob);
per.putExtra("phone", phn);
per.putExtra("address", add12);
per.putExtra("zip", zip1);
per.putExtra("city", tow);
per.putExtra("dateofbirth", dateob);
startActivity(per);
}
}
});
Now even after I have both of the edittext field to be same it is still showing me the alert dialog that passwords do not match.Please help me out here.Thanks in advance.
You cannot compare strings using = or !=, use equals instead
else if(!conpass.getText().toString().equals(pass.getText().toString()) )
Try this function:
public boolean isPasswordMatching(String password, String confirmPassword) {
Pattern pattern = Pattern.compile(password, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(confirmPassword);
if (!matcher.matches()) {
// do your Toast("passwords are not matching");
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With