Password and confirm password validation in Android Studio

Posted on

Problem

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();
            }
        });
    }
}

Solution

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 {
    // ...
}

Leave a Reply

Your email address will not be published. Required fields are marked *