ahsen
ahsen

Reputation: 39

Empty editText field shows error on all fields [Android]

i have five editText fields and i want to validate if there is empty editText. my code is working, the problem is that when there is one empty field, error shows on all fields.

here is my code:

            String one = firstname.getText().toString();
            String two = lastname.getText().toString();
            String three = email.getText().toString();
            String four = address.getText().toString();
            String five = mobile.getText().toString();

            if(!TextUtils.isEmpty(one) && !TextUtils.isEmpty(two) &&
                    !TextUtils.isEmpty(three) &&!TextUtils.isEmpty(four) &&
                    !TextUtils.isEmpty(five)) {
                Intent i = new Intent(main.this, next.class);
                i.putExtra("first", one);
                i.putExtra("last", two);
                i.putExtra("email", three);
                i.putExtra("mobile", four);
                i.putExtra("address", five);
                startActivity(i);
            } else {
                firstname.setError("First name cannot be empty");
                lastname.setError("last name cannot be empty");
                email.setError("email cannot be empty");
                address.setError("address cannot be empty");
                mobile.setError("mobile cannot be empty");

            }

Upvotes: 0

Views: 1936

Answers (5)

Subramanian Mariappan
Subramanian Mariappan

Reputation: 3886

Try,

Boolean valid = true;
if ( TextUtils.isEmpty(one) ) {
    firstname.setError("First name cannot be empty");
    valid = false;
} 

if ( TextUtils.isEmpty(two) ) {
    lastname.setError("last name cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(three) ) {
    email.setError("email cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(four) ) {
    address.setError("address cannot be empty");
    valid = false;
}

if ( TextUtils.isEmpty(five) ) {
    mobile.setError("mobile cannot be empty");
    valid = false;
}

if ( valid ) {
   Intent i = new Intent(main.this, next.class);
   i.putExtra("first", one);
   i.putExtra("last", two);
   i.putExtra("email", three);
   i.putExtra("mobile", four);
   i.putExtra("address", five);
   startActivity(i);
}

Upvotes: 1

Afsar Ahamad
Afsar Ahamad

Reputation: 1997

Here is one simple example where you can reuse code in this particular case.


...
//Usage
 if(validate(firstname,"First name") && validate(lastname, "last name") &&
                    validate(email,"email") &&validate(address,"address") &&
                    validate(mobile,"Mobile")) {
                Intent i = new Intent(main.this, next.class);
                i.putExtra("first", one);
                i.putExtra("last", two);
                i.putExtra("email", three);
                i.putExtra("mobile", four);
                i.putExtra("address", five);
                startActivity(i);
            }
        // No need of else statement

....
// Create this method somewhere in your activity
boolean validate(EditText editText, String label){

     if(TextUtils.isEmpty(editText.getText().toString())){
           editText.setError(label+" cannot be empty");
           return false;
     }
   return true;
}

Upvotes: 0

Irfan Yaqub
Irfan Yaqub

Reputation: 140

Here is the basic logic where you are doing wrong, your code is saying if anyone from these four ( one, two , three , four ) is empty then your if statement will o execute, else statement will execute, so in else all your edit text will show error. So now you need to check like this,

    if (!TextUtils.isEmpty(one)) {
        i.putExtra("first", one);
    } else {
        firstname.setError("First name cannot be empty");
        return;
    }

    if (!TextUtils.isEmpty(two)) {
        i.putExtra("last", two);

    } else {
        lastname.setError("last name cannot be empty");
        return;
    }
    if (!TextUtils.isEmpty(three)) {
        i.putExtra("last", three);

    } else {
        lastname.setError("last name cannot be empty");
      return;
    }
     if (!TextUtils.isEmpty(four)) {
        i.putExtra("last", four);

    } else {
        lastname.setError("last name cannot be empty");
       return;
    }
   if (!TextUtils.isEmpty(five)) {
        i.putExtra("last", five);

    } else {
        lastname.setError("last name cannot be empty");
        return;
    }

Upvotes: 0

Dipankar Baghel
Dipankar Baghel

Reputation: 2069

Because in else case you are setting error on every field -

            firstname.setError("First name cannot be empty");
            lastname.setError("last name cannot be empty");
            email.setError("email cannot be empty");
            address.setError("address cannot be empty");
            mobile.setError("mobile cannot be empty");

-------------------------above is wrong ---------------------

For specific try separating the if case like below -

if ( TextUtils.isEmpty(one) ) {
    firstname.setError("First name cannot be empty");
}
if ( TextUtils.isEmpty(two) {
    lastname.setError("last name cannot be empty");
}

......and so on.

Hope this will help you.

Upvotes: 0

Antonis Radz
Antonis Radz

Reputation: 3097

you doing if checks on everything at once, do it separately like this

 Intent i = new Intent(main.this, next.class);

        if (!TextUtils.isEmpty(one)) {
            i.putExtra("first", one);
        } else {
            firstname.setError("First name cannot be empty");
        }

        if (!TextUtils.isEmpty(two)) {
            i.putExtra("last", two);

        } else {
            lastname.setError("last name cannot be empty");
        }
        ......

Upvotes: 0

Related Questions