user218554
user218554

Reputation: 59

Password validation confirmation in android

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.

Upvotes: 3

Views: 23931

Answers (3)

santhosh
santhosh

Reputation: 31

refer this to get clue on handling this

its simple filtration

in if condition it checks old password and new password are not null or empty and checks both are not same in else if new password and old password are not null or empty and checks both lengths are equal and verifies both strings are equal

You can choose this if you Prefer simple validation

    @Override
    public void afterTextChanged(Editable s) {

        System.out.println(s.toString());
        String oldPass = oldPassword.getText().toString();
        String newPass = newPassword.getText().toString();
        String confirmPass = confirmPassword.getText().toString();
        if (!oldPass.equals("") && !newPass.equals("") && oldPass.equals(newPass)) {
            Toast.makeText(getContext(), "Choose Different Password than Old Password", Toast.LENGTH_SHORT).show();
        }
        else if (!newPass.equals("") && !confirmPass.equals("") && !newPass.equals(confirmPass) && newPass.length()==confirmPass.length())
        {
            Toast.makeText(getContext(), "Choose same as New Password", Toast.LENGTH_SHORT).show();
        }
    }

Upvotes: 0

user4870980
user4870980

Reputation:

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

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You cannot compare strings using = or !=, use equals instead

else if(!conpass.getText().toString().equals(pass.getText().toString()) )

Upvotes: 11

Related Questions