Edit Oct 22, 2012
The library now supports annotations, you can validate your fields just by adding annotations. Here is an example code snippet.
@Required(order = 1)
private EditText fieldEditText;
@Checked(order = 2, message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;
@TextRule(order = 3, minLength = 3, message = "Enter atleast 3 characters.")
@Regex(order = 4, pattern = "[A-Za-z]+", message = "Should contain only alphabets")
private TextView regexTextView;
@Password(order = 5)
private EditText passwordEditText;
@ConfirmPassword(order = 6)
private EditText confirmPasswordEditText;
The order attribute is mandatory and specifies the order in which the fields will be validated. This is required because the order or the fields and annotations are not preserved in the compiled DEX files. There are also other annotations such as
@NumberRule, etc., You can download the jar from here and add it to your Android
I have authored a library for validation. Here is the associated blog and the project. I have sucessfully used it in production applications and it currently satisfies most of the common scenarios that we face in validation forms for Android. There are rules that come out of the box and if you need to write your own, you can do that by writing your own Rule.
Here is a snippet that illustrates the use of the library.
validator.put(nameEditText, Rules.required("Name is required."));
validator.put(nameEditText, Rules.minLength("Name is too short.", 3));
validator.put(emailEditText, Rules.regex("Email id is invalid.", Rules.REGEX_EMAIL, trim));
validator.put(confirmPwdEditText, Rules.eq("Passwords don\'t match.", pwdEditText);
There are also
and rules that allow you to perform
|| operations on several rules. There is also a
compositeAnd rule that allows you to perform validations between several Views.
If any of those seem to be insufficient, you can always write your own rule by extending the Rule class.