0
\$\begingroup\$

I just wrote this password and password confirmation validation code function that checks to see if a password is empty or not, and is it match (password = confirmPassword). I'm not sure this is the best way to do it, but for now it dont works really well.

private void checkDataEntered()
{
    if(isEmpty(RegisterFirstName))
    {
        RegisterFirstName.setError("You must enter first name to register");
    }

    if(isEmpty(RegisterLastName))
    {
        RegisterLastName.setError("Last name is required");
    }

    if(isEmail(RegisterEmail) == false)
    {
        RegisterEmail.setError("Enter your valid email.");
    }

    if(isEmpty(RegisterPassword))
    {
        RegisterPassword.setError("Enter your password.");
    }

    if(isEmpty(RegisterConfirmPassword))
    {
        RegisterConfirmPassword.setError("Enter your confirmation password");

        if (!RegisterConfirmPassword.equals(RegisterPassword))
        {
            Toast.makeText(Signup.this, "Password do not match", Toast.LENGTH_SHORT).show();
        }
    }

    else
    {
        loadingBar.setTitle("Creating New Account");
        loadingBar.setMessage("Please wait while we are creating account for you.");
        loadingBar.show();

        String email = null;
        String password = null;

        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
        {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task)
            {
                if(task.isSuccessful())
                {
                    Toast.makeText(Signup.this, "You have successfully signed up", Toast.LENGTH_SHORT).show();
                    Intent mainIntent = new Intent(Signup.this, MainActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                }
                else
                {
                    Toast.makeText(Signup.this, "Error occured, please try again.", Toast.LENGTH_SHORT).show();
                }
                loadingBar.dismiss();
            }
        });
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Does this actually work when the form is correctly filled? The call mAuth.createUserWithEmailAndPassword(email, password) is suspicious, because at that point email and password are both null. I don't see how the task in the onComplete callback can be successful. \$\endgroup\$
    – janos
    Commented Aug 5, 2018 at 7:54

1 Answer 1

2
\$\begingroup\$

Inconsistent UX

Most validation errors call setError of the relevant input field. An exception is when the confirmation password doesn't match the first entry, that pops up a toast. It would be better to handle validation errors consistently, by using setError for mismatched confirmation password.

It's ok to use a toast for the result of the registration, when all form fields are valid.

Creating account with invalid input

Before calling mAuth.createUserWithEmailAndPassword, the only validation that's really enforced is that the confirmation password is not empty. That's strange. It would be better to call this only when all entries of the form are filled and valid.

Style issues

A convention in Java is to use camelCase for variable names. For example RegisterConfirmPassword should be registerConfirmPassword.

Instead of if (a == false) { it's more natural to write if (!a) {.

It's a convention in Java to place braces like this:

if (cond) {
    // ...
} else {
    // ...
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.