0

I am trying to check whether the user has enter his/her mobile number in the EditText on the button click event, but I am not able to check whether the EditText is empty or not when control comes to "editRegMobile". When I click on the button it doesn't show the message in the Toast. I also want to check the format of the mobile number i.e "+91 999999999" country code. I am able to check for other EditText but not for Mobile EditText.

xml code:

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/editRegMobile"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/sign_in_up_textbox"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="35dp"
            android:textSize="16sp"
            android:letterSpacing="0.08"
            android:singleLine="true" />

Java code:

btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editRegName != null && TextUtils.isEmpty(editRegName.getText().toString().trim())) {
                    editRegName.setError("Required!");
                    editRegName.requestFocus();
                } else if (editRegEmail != null && TextUtils.isEmpty(editRegEmail.getText().toString())) {
                    editRegEmail.setError("Required!");
                    editRegEmail.requestFocus();
                } else if (!(editRegEmail != null && TextUtils.isEmpty(editRegEmail.getText().toString()))) {
                    if (!Patterns.EMAIL_ADDRESS.matcher(editRegEmail.getText().toString()).matches()) {
                        editRegEmail.setError("Invalid Format!");
                        editRegEmail.requestFocus();
                    }
                } else if ((editRegMobile != null) && TextUtils.isEmpty(editRegMobile.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "Please enter mobile number!", Toast.LENGTH_SHORT).show();
                } else if (!(editRegMobile != null && TextUtils.isEmpty(editRegMobile.getText().toString()))) {
                    if (!Patterns.PHONE.matcher(editRegMobile.getText().toString()).matches()) {
                        Toast.makeText(getApplicationContext(), "Please enter valid mobile number!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
Arang
  • 15
  • 7
  • Possible duplicate of http://stackoverflow.com/questions/27263746/how-to-validate-phone-number-format or http://stackoverflow.com/questions/22505336/email-and-phone-number-validation-in-android – Jay Rathod Aug 20 '16 at 13:32
  • 1
    Possible duplicate of [Check if EditText is empty.](http://stackoverflow.com/questions/6290531/check-if-edittext-is-empty) – sumandas Aug 20 '16 at 13:33
  • you don't need to do editRegMobile.getText().toString() just do editRegMobile.getText() – Muddassir Ahmed Aug 20 '16 at 13:35
  • This is what you're looking for http://stackoverflow.com/a/25156934/2308683 – OneCricketeer Aug 20 '16 at 13:38
  • `TextUtils.isEmpty` already checks null OR empty strings, by the way. (ideally your view object shouldn't be null) And you only need an `else` statement. No need to negate the entire `if` condition – OneCricketeer Aug 20 '16 at 13:39

2 Answers2

0

Simple to check for empty :

edittext.getText().toString().length() == 0
Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62
0

You can make

if(editRegMobile.getText().length() == 0)

Or

editRegMobile.getText().toString().length()

The second way cost some performance because need convert to String first

Martin Sch
  • 64
  • 2