如果两个或其中一个文本字段为空,我如何验证电子邮件和密码字段?

时间:2018-02-18 00:22:23

标签: java android firebase firebase-authentication

Can anyone help me how to put checks in this code. How can I validate this sign in function with a validation so that my application gives an error with the text fields are null

private void loginUser(final String email, final String password) {
    auth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if(!task.isSuccessful())
                    {
                        if(password.length() < 6)
                        {
                            Snackbar snackBar = Snackbar.make(activity_main,"Password length must be over 6",Snackbar.LENGTH_SHORT);
                            snackBar.show();
                        }

                    }
                    else{
                        startActivity(new Intent(MainActivity.this,DashBoard.class));
                    }
                }
            });

1 个答案:

答案 0 :(得分:1)

if (!email.equals("") && !password.equals("")){
    // this part will execute only when email and password are not null/empty.
    // and the app can continue to make an attempt for login.
    loginUser(email,password);
}else{
    // if here... it means email or password field is empty or not being
    // preserved correctly.
 // so you can:
    // 1. display msg here... for ex "pls fill all fields!"
    // 2. or handle in any way you like
}

单击登录按钮时执行此操作。 希望这能解决问题。 :)