【发布时间】: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) {
}
});
}}
谢谢!
【问题讨论】: