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:
- Simple to Use.
- Easily Customizable
- 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 'com.android.tools.build:gradle:2.3.3'
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'
}
Examples:
The following example is used to validate email id with EditText.
@BindView(R.id.email)
@NotEmpty
@Email
EditText emailEditText;
@NotEmpty is used to make mandatory input of EditText.
@Email is used to validate Email input.
@BindView used for Butter Knife
@Email is used to validate Email input.
@BindView used for Butter Knife
The following example is used to validate Password with Confirm Password.
@BindView(R.id.password)
@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
EditText passwordEditText;
@BindView(R.id.conf_password)
@ConfirmPassword
EditText confirmPasswordEditText;
@Password is used to validate password with ALPHA_NUMERIC_MIXED_CASE_SYMBOLS
@ConfirmPassword used to match @Password input with Confirm Password input.
@ConfirmPassword used to match @Password input with Confirm Password input.
The following example is used to validate with custom pattern and custom message.
@BindView(R.id.phone)
@Pattern(regex = "^[7-9][0-9]{9}$", message = "Invalid Mobile Number")
EditText phoneEditText;
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 {
...
@Override
public void onValidationSucceeded() {
Toast.makeText(this, "Validation Success", Toast.LENGTH_SHORT).show();
}
@Override
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); validator.setValidationListener(this); ... @OnClick(R.id.submit) public void onButtonClick(View view) { validator.validate(); }
Download Code:
You can the download code for this post from Github. If you like this, tutorial star it on Github.
Follow Us
Were this world an endless plain, and by sailing eastward we could for ever reach new distances