I need to do form input validation on a series of EditTexts. I'm using OnFocusChangeListeners to trigger the validation after the user types into each one, but this doesn't behave as desired for the last EditText.
If I click on the "Done" button while typing into the final EditText then the InputMethod is disconnected, but technically focus is never lost on the EditText (and so validation never occurs).
What's the best solution?
Should I be monitoring when the InputMethod unbinds from each EditText rather than when focus changes? If so, how?
In main.xml file
You can put the following attrubute to validate only alphabatics character can accept in edittext.
Do this :
If you want nice validation popups and images when an error occurs you can use the
setError
method of theEditText
class as I describe hereI needed to do intra-field validation and not inter-field validation to test that my values were unsigned floating point values in one case and signed floating point values in another. Here's what seems to work for me:
Note, you must not have any spaces inside "numberSigned|numberDecimal". For example: "numberSigned | numberDecimal" won't work. I'm not sure why.
Why don't you use
TextWatcher
?Since you have a number of
EditText
boxes to be validated, I think the following shall suit you :android.text.TextWatcher
interfaceafterTextChanged(Editable s)
method as followsThe
Editable s
doesn't really help to find which EditText box's text is being changed. But you could directly check the contents of the EditText boxes likein the same method. I hope I'm clear and if I am, it helps! :)
EDIT: For a cleaner approach refer to Christopher Perry's answer below.
I wrote a class that extends EditText which supports natively some validation methods and is actually very flexible.
Current, as I write, natively supported through xml attributes validation methods are:
You can check it out here
Hope you enjoy it :)
This looks really promising and just what the doc ordered for me:
EditText Validator
custom validators plus these built in: