Android Form Validation using Saripaar Validator

In this tutorial, we will learn how to validate forms in Android using Saripaar Library. Android Saripaar is a simple, feature-rich and powerful rule-based UI form validation library for Android. It is the Simplest UI validation library available for Android. To learn about Butter Knife, read my previous article.

Advantages of using Saripaar:

  1. Simple to Use.
  2. Easily Customizable
  3. Supports all annotation services in Android like Butter Knife, Android Annotations, Robo Guice, etc.,

Project Setup:

In this tutorial, I have used Saripaar with Butter Knife. Open Project Level Gradle file and paste the following
dependencies {
 classpath ''
 classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
Open App level Gradle file and add the following dependencies
apply plugin: 'com.jakewharton.butterknife'
dependencies {
    compile 'com.mobsandgeeks:android-saripaar:2.0.2'
    compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'


The following example is used to validate email id with EditText. 
EditText emailEditText;
@NotEmpty is used to make mandatory input of EditText.
@Email is used to validate Email input.
@BindView used for Butter Knife
The following example is used to validate Password with Confirm Password.
@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
EditText passwordEditText;
EditText confirmPasswordEditText;
@Password is used to validate password with ALPHA_NUMERIC_MIXED_CASE_SYMBOLS
@ConfirmPassword used to match @Password input with Confirm Password input.
The following example is used to validate with custom pattern and custom message.
@Pattern(regex = "^[7-9][0-9]{9}$", message = "Invalid Mobile Number")
EditText phoneEditText;
@Pattern is used to validate custom pattern.
regex is used to set custom pattern.
message used to set custom message.

Validation Process:

You can set validation listener like below.
public class MainActivity extends AppCompatActivity 
   implements Validator.ValidationListener {

    public void onValidationSucceeded() {
        Toast.makeText(this, "Validation Success", Toast.LENGTH_SHORT).show();

    public void onValidationFailed(List errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
The Form validated with the following code
validator = new Validator(this);
public void onButtonClick(View view) {

Download Code:

You can the download code for this post from Github. If you like this, tutorial star it on Github.

Share this

Related Posts

Next Post »