Use addValidation() to attach rules to your views. Supported rule types include:
- Regex/Patterns: Pass a regex string, a
java.util.regex.Pattern, or Android Patterns (e.g., android.util.Patterns.EMAIL_ADDRESS). - Ranges: Use
Guava#Range (e.g., Range.closed(min, max)). - Confirmation: To validate that a field matches another (e.g., password confirmation), pass the ID of the target field as the second argument to
addValidation(). - Simple Custom Validation: Implement
SimpleCustomValidation for logic that only needs to return a boolean based on the input string. - Advanced Custom Validation: Implement
CustomValidation, CustomValidationCallback, and NewErrorReset for full control over how errors are displayed and cleared.
Note for Fragments: When using AwesomeValidation in a Fragment, call .validate() inside onActivityCreated rather than onCreateView to ensure the lifecycle is handled correctly.
// Regex and Patterns
mAwesomeValidation.addValidation(activity, R.id.edt_name, "[a-zA-Z\\s]+", R.string.err_name);
mAwesomeValidation.addValidation(activity, R.id.edt_email, android.util.Patterns.EMAIL_ADDRESS, R.string.err_email);
// Ranges
mAwesomeValidation.addValidation(activity, R.id.edt_year, Range.closed(1900, Calendar.getInstance().get(Calendar.YEAR)), R.string.err_year);
// Confirmation (matches R.id.edt_password)
mAwesomeValidation.addValidation(activity, R.id.edt_password_confirmation, R.id.edt_password, R.string.err_password_confirmation);
// TextInputLayout (pass the layout, not the EditText)
mAwesomeValidation.addValidation(activity, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email);
// Simple Custom Validation
mAwesomeValidation.addValidation(activity, R.id.edt_birthday, new SimpleCustomValidation() {
@Override
public boolean compare(String input) {
// return true if valid, false otherwise
return true;
}
}, R.string.err_birth);