There is iOS US2FormValidator library for user input validation (see the picture below). I think that library is better than the default of just popping an alert when something doesn't validate.
I'm looking for how to do such things on Android. Are there some Android analogs of US2FormValidator?
The pop-up effect you have shown on your screenshot can be achieved using Android's built-in setError(String)
method on EditText
widgets.
Also, you can leverage the power of annotations using the Android Saripaar library that I've authored.
first add the library:
compile 'com.mobsandgeeks:android-saripaar:2.0.2'
The library is very simple to use. In your activity annotate the View
references you would like to validate as in the following example.
@Order(1) private EditText fieldEditText; @Order(2) @Checked(message = "You must agree to the terms.") private CheckBox iAgreeCheckBox; @Order(3) @Length(min = 3, message = "Enter atleast 3 characters.") @Pattern(regex = "[A-Za-z]+", message = "Should contain only alphabets") private TextView regexTextView; @Order(4) @Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS) private EditText passwordEditText; @Order(5) @ConfirmPassword private EditText confirmPasswordEditText;
The order
attribute specifies the order in which the fields have to be validated.
In your onCreate()
method instantiate a new Validator
object. and call validator.validate()
inside any of your event listeners.
You'll receive callbacks on onSuccess
and onFailure
methods of the ValidationListener
.
If you want to show a pop-up as show in the image above then do the following,
public void onValidationFailed(View failedView, Rule<?> failedRule) { if (failedView instanceof Checkable) { Toast.makeText(this, failedRule.getFailureMessage(), Toast.LENGTH_SHORT).show(); } else if (failedView instanceof TextView) { TextView view = (TextView) failedView; view.requestFocus(); view.setError(failedRule.getFailureMessage()); } }
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With