Java android中的密码验证

Java android中的密码验证,java,android,string-comparison,Java,Android,String Comparison,我有一个活动,我想在其中验证确认密码字段。这是我的代码-: nt.setOnClickListener(new OnClickListener() { public void onClick(View v) { if(email.getText().toString().equals("")){ AlertDialog alertDialog = new AlertDialog.Builder(Registration.this).cre

我有一个活动,我想在其中验证确认密码字段。这是我的代码-:

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

    }
});  

现在,即使我将两个edittext字段都设置为相同,它仍然会显示密码不匹配的警报对话框。请在此帮助我。提前感谢。

您不能使用
=
比较字符串=,改用
等于

else if(!conpass.getText().toString().equals(pass.getText().toString()) )
尝试此功能:

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

参考此以获取处理此问题的线索

它的过滤很简单

中,如果
条件下,它检查
旧密码
新密码
是否为
null
为空
,并检查两者是否相同 在else中,如果
新密码
旧密码
不为
,则检查两个长度是否相等,并验证两个字符串是否相等

如果您喜欢简单验证,可以选择此选项

    @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();
        }
    }

虽然这个代码片段是受欢迎的,并可能提供一些帮助,但它将是如何以及为什么解决这个问题。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。