【问题标题】:Matching two EditText entries doesn't work with asterisks (fieldOne.matches(fieldTwo))匹配两个 EditText 条目不适用于星号 (fieldOne.matches(fieldTwo))
【发布时间】:2020-03-19 06:25:59
【问题描述】:

我正在使用存储在 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) {

        }
    });
}}

谢谢!

【问题讨论】:

    标签: java android passwords


    【解决方案1】:

    password.matches(repassword) 期望正则表达式被传递并且你传递一个字符串

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

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

    【讨论】:

    • 您的提示使我阅读了有关 equals() 和 equalsIgnoreCase() 方法的更多信息,对此我表示感谢。我不能使用 equalsIgnoreCase() 因为显然我希望我的密码区分大小写,但我确实使用了带有“!”的 equals() :)
    • 啊,我忘了你需要区分大小写,所以你是对的
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多