匹配两个EditText条目不适用于星号(fieldOne.matches(fieldTwo))

时间:2019-11-23 10:51:59

标签: java android passwords

我正在进行注册活动,将用户电子邮件和密码存储在Firebase数据库中。除了一件事情外,一切工作都非常完美:您可以看到代码确实允许在密码中使用特殊字符(即使您只需要至少包含8个字符(包括字母和数字)也可以)如果我尝试使用此处编码的“ abcd1234 *”之类的密码,则会收到“密码不匹配” Toast错误消息:

 }
    else if (!(password.matches(repassword))){
        Toast.makeText(this, "Passwords don't match", Toast.LENGTH_LONG).show();
    }

如果我尝试使用相同的确切密码而不包含星号,则可以正常工作。就像匹配两个特殊字符时,代码感到困惑。这是完整的代码:

public class SignUpActivity extends AppCompatActivity {
private Button CreateAccountButton;
private EditText InputEmail, InputPassword, ReInputPassword;
private TextView terminiRead;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);

    CreateAccountButton = findViewById(R.id.signup_btn);
    InputEmail = findViewById(R.id.signup_email_input);
    InputPassword = findViewById(R.id.signup_psw_input);
    ReInputPassword = findViewById(R.id.signup_repsw_input);
    terminiRead = findViewById(R.id.termini);

    terminiRead.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SignUpActivity.this,TerminiDiServizio.class);
            startActivity(intent);
        }
    });

    CreateAccountButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreateAccount();
        }
    });
}


private void CreateAccount() {


    String email = InputEmail.getText().toString();
    String password = InputPassword.getText().toString();
    String repassword = ReInputPassword.getText().toString();

    Pattern PSWPattern = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d@$!%*#?&]{8,}$");

    if (TextUtils.isEmpty(email)){
        Toast.makeText(this, "Insert an e-mail address", Toast.LENGTH_LONG).show();
    }
    else if (!(Patterns.EMAIL_ADDRESS.matcher(email).matches())){
       Toast.makeText(this,"Invalid e-mail address", Toast.LENGTH_LONG).show();
    }
    else if (TextUtils.isEmpty(password)){
        Toast.makeText(this, "Insert a password", Toast.LENGTH_LONG).show();
    }
    else if (TextUtils.isEmpty(repassword)){
        Toast.makeText(this, "Reinsert your password", Toast.LENGTH_LONG).show();
    }
    else if (!(password.matches(PSWPattern.pattern()))){
        Toast.makeText(this, "The password length must be at least 8 and containing at least a number and a letter", Toast.LENGTH_LONG).show();
    }
    else if (!(password.matches(repassword))){
        Toast.makeText(this, "Passwords don't match", Toast.LENGTH_LONG).show();
    }
    else {
        ValidateAccount(email,password,repassword);
    }
}

private void ValidateAccount(final String email, final String password, final String repassword) {
    final DatabaseReference RootRef;
    RootRef = FirebaseDatabase.getInstance().getReference();

    RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            if (!(dataSnapshot.child("User").child(email.replace(".",",")).exists()))
            {
                HashMap<String, Object> userdataMap = new HashMap<>();
                userdataMap.put("email", email.replace(".",","));
                userdataMap.put("password", password);
                userdataMap.put("repassword", repassword);

                RootRef.child("User").child(email.replace(".",",")).updateChildren(userdataMap)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()){
                                    Toast.makeText(SignUpActivity.this,"Thanks for signing up", Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(SignUpActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                }
                                else
                                {
                                    Toast.makeText(SignUpActivity.this, "An error occurred, retry", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
            }

            else
            {
            Toast.makeText(SignUpActivity.this, "E-mail address is already in use", Toast.LENGTH_LONG).show();
        }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}}

谢谢!

2 个答案:

答案 0 :(得分:1)

password.matches(repassword)希望通过正则表达式,并且您将其传递为字符串

  • password.equalsIgnoreCase(repassword)(不区分大小写)

  • password.equals(repassword)区分大小写

答案 1 :(得分:1)

找到解决方案:只需使用equals()方法而不是matchs()方法:

(!(password.equals(repassword)));

别忘了! (逻辑相反),如果两个条目不匹配,则在开始时尝试使某些事情发生。 :)