user3197707
user3197707

Reputation: 29

How to validate EditText?

I am doing one SMS App.. in which i am trying to validate edittext(i.e. if user will not enter the phone number then it will show please enter the number), but it is not executing those part.. Please check it and give me some suggestion....My code is here.. Thank you in advance..

public class MainActivity extends Activity {

    Button btnSend;
    EditText txtPhoneNo;
    EditText txtSMS; 
    String phoneNo, SMS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnSend=(Button) findViewById(R.id.buttonSend);
        txtPhoneNo=(EditText) findViewById(R.id.editTextPhoneNo);
        txtSMS=(EditText) findViewById(R.id.editTextSMS);

        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                phoneNo=txtPhoneNo.getText().toString();
                SMS=txtSMS.getText().toString();

                if(phoneNo.equals(" ") || phoneNo == null) {
                    Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
                }
                else {
                MyAsync ma = new MyAsync();
                ma.execute();
                }
            }
        });
    }

    class MyAsync extends AsyncTask<Void,Void,Void> {

        ProgressDialog pd;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pd = ProgressDialog.show(MainActivity.this, "Wait!", "Sending...");
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

                try {
                    SmsManager smsManager=SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNo, null, SMS, null, null);

            } catch (Exception e) {             
                    Toast.makeText(getApplicationContext(), "SMS faild, please try again later!",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            pd.cancel();
            Toast.makeText(getApplicationContext(),"Sent",Toast.LENGTH_LONG).show();
        }

    }


}

Upvotes: 2

Views: 155

Answers (4)

Vigbyor
Vigbyor

Reputation: 2604

Instead of these code,

if(phoneNo.equals(" ") || phoneNo == null) {
                    Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
                }

Try following code,

// Trim the variable

phoneNo=txtPhoneNo.getText().toString().trim();

// Then condition part

if(phoneNo.length() == 0 && phoneNo != null) {
                    Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
                }

Upvotes: 1

InnocentKiller
InnocentKiller

Reputation: 5234

To do this no need to take String and pass the content of edit-text into string. You can directly check by following code.

           if(txtPhoneNo.length()<=0) 
            {
                Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
            }
            else {
            MyAsync ma = new MyAsync();
            ma.execute();
            }

Upvotes: 1

phemt.latd
phemt.latd

Reputation: 1803

If you want to try something more, don't check validation on click button. Do it before with: TextWatcher

Your activity implements android.text.TextWatcher interface You add TextChanged listeners to you EditText boxes

txt1.addTextChangedListener(this);

Of the overridden methods, you could use the afterTextChanged(Editable s) method as follows:

@Override
public void afterTextChanged(Editable s) {
// validation code goes here
}

You could directly check the contents of the EditText boxes like

String txt1String = txt1.getText().toString();
// Validate txt1String

Upvotes: 1

anirvan
anirvan

Reputation: 4877

You are using the following code to determine whether the user entered anything—

phoneNo.equals(" ") || phoneNo == null

Firstly, when the user keeps the EditText empty, it will be a 0-length string "" and NOT a space " ", which you're testing for.

Secondly, why are you making the null check? Is it possible that your layout won't have that EditText field? And, if that's possible, the null check should be the first thing to check. Because, if it can be null then your app will certainly crash, since you're testing for equals() on a null object before the null check!

Upvotes: 2

Related Questions