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();
 }
 });
 }
}
asked Aug 5, 2018 at 5:48
\$\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\$ 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 {
 // ...
}
answered Aug 5, 2018 at 8:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.