1

I am creating a Sign-Up form.

I have used setError() to set error messages in all EditText s. now I want to check that whether an EditText has error message or not when I click on the submit button.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78

3 Answers3

6

To check text after it has been written but before clicking register/submit button on your page, you need to use TextWatcher

After setting up error with setError(), you need to check if getError() returns null as well as editText.getText().length() returns 0 then perform action like Toast or anything.

E.g:

onClick(View v){
    if(editText.getError() != null || editText.getText().length() == 0){
        ................(Perform Toast or something for clearing error)
    }else{
        .... your action to perform on button click
    }
Krups
  • 407
  • 6
  • 13
1

You could use the getError() method for the EditText and check if it returns null. Although a better way would be to just set a boolean isValid on errors and check against that

ashkhn
  • 1,642
  • 1
  • 13
  • 18
1

Right approach to achieve an error less submission of your form will be following:

  1. on click of submit check whether entered text is a correct email.
  2. if its not correct show error using setError.
  3. if its correct go ahead and submit your form.

you need not to check explicitly if error is set or not as upon next submit field will be re-verified anyways.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • actually my objective is to display the errors after the typing but before the submit button is pressed – Nikhil Bhardwaj Oct 03 '15 at 22:03
  • you [can use `onFocusChange`](http://stackoverflow.com/a/10627231/1529129) to achieve that. still it is a good idea to keep validation on submit button. – Rahul Tiwari Oct 03 '15 at 22:13