Skip to main content
4 of 4
deleted 1 character in body; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Validating multiple EditText fields

I am using the following method to validate if user has given any input or left an EditText empty. I am concerned about the return statement in particular, because my IDE keeps complaining about it.

Environment: Android Studio 1.0

public void clientsideauth() {

        if (TextUtils.isEmpty(edt_name.getText().toString().trim())) {
            onShake(edt_name, "Please enter a valid name");
            return;
        } else {
            name = edt_name.getText().toString().trim();
        }
        if (isValidEmail(edt_email.getText().toString().trim())) {
            email = edt_email.getText().toString().trim();
        } else {
            onShake(edt_email, "Please enter a valid email ID");
            return;
        }

        if (((edt_password.getText().length() < 6) || (TextUtils.isEmpty(edt_password.getText().toString().trim())))) {
            onShake(edt_password, "Password must be at least six characters long");
            return;
        } else {
            pwd = edt_password.getText().toString().trim();
        }

        if (TextUtils.isEmpty(edt_password1.getText().toString().trim())) {
            onShake(edt_password1, "Not valid");
            return;
        } else {
            pwd1 = edt_password1.getText().toString().trim();
        }


        if (pwd.equals(pwd1)) {
            startbackgroundtask("formBased");
        } else {
            onShake(edt_password, "Passwords do not match");
            onShake(edt_password1, "Passwords do not match");
            return;
        }
    }

The return type of the method is void, because I don't want any return from this, I only want to stop execution if validation fails. However my IDE complains about the last return statement in the last if-else construct. I am wondering if I should in fact change the return type from void to boolean and maintain an array value for each form field. Later on I can check if all the array values are true or not.

The above function gets called on the Submit button and is further processed to the server.

Junaid
  • 217
  • 1
  • 4
  • 10