gpt4 book ai didi

java - 我的类(class)中可以包含哪些代码行,以便对电子邮件和密码进行验证?

转载 作者:行者123 更新时间:2023-11-29 18:35:58 25 4
gpt4 key购买 nike

我已经编写了以下代码行,我需要在其中包含电子邮件和密码的错误验证检查。错误的邮件/密码输入时屏幕上的输出应采用 Toast.makeText() 格式。所以这是下面的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public void clickRegister(View view) {

startActivity(new Intent(MainActivity.this, RegisterActivity.class));

}

private Button buttonSignin;
private EditText emailAdd;
private EditText passAdd;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth=FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()!=null){
//Remote control activity
finish();
startActivity(new Intent(getApplicationContext(),RemoteControl.class));
}
buttonSignin = (Button) findViewById(R.id.logIn);
emailAdd = (EditText) findViewById(R.id.emailAddress);
passAdd = (EditText) findViewById(R.id.passWord);

buttonSignin.setOnClickListener(this);


}
private void userLogin(){
String email=emailAdd.getText().toString().trim();
String password=passAdd.getText().toString().trim();
if(TextUtils.isEmpty(email)){
//Email is empty!
Toast.makeText(this,"Please Enter Email",Toast.LENGTH_SHORT).show();
//Stop function from executing further
return;
}

if (TextUtils.isEmpty(password))
{
//Password is empty
Toast.makeText(this,"Please Enter Password",Toast.LENGTH_SHORT).show();
return;
}


firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//start profile activity
finish();
startActivity(new Intent(getApplicationContext(),RemoteControl.class));
}
}
});
}
@Override
public void onClick(View view) {
if(view==buttonSignin)
{
userLogin();
}

}
}

我希望以 Toast.maketext() 的形式为每一行错误输入输出 Incorrect EmailIncorrect password

最佳答案

根据 Firebase Auth documentation , firebase 抛出一个 FirebaseAuthException 错误。您在这里寻找的子类是:

  • FirebaseAuthInvalidCredentialsException - 如果密码不正确
  • FirebaseAuthInvalidUserException - 如果帐户不存在

并将其应用为:

firebaseAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
//start profile activity
finish();
startActivity(new Intent(getApplicationContext(),RemoteControl.class));
}

else if(task.getException() instanceof
FirebaseAuthInvalidCredentialsException){
Toast.makeText(context, "wrong password", Toast.LENGTH_SHORT).show();
}else if(task.getException() instanceof FirebaseAuthInvalidUserException){
Toast.makeText(context, "no user with such email", Toast.LENGTH_SHORT).show();
}
}
});

但是如前所述,您不想通知您的用户电子邮件不存在,而只是在电子邮件格式错误时通知他们,为此您可以使用如下正则表达式模式:

if(TextUtils.isEmpty(email)){
//Email is empty!
Toast.makeText(this,"Please Enter Email",Toast.LENGTH_SHORT).show();
//Stop function from executing further
return;
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
Toast.makeText(context,"Email is badly formatted",Toast.LENGTH_SHORT).show();
}

关于java - 我的类(class)中可以包含哪些代码行,以便对电子邮件和密码进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com