The Easiest way to validate forms in an Android application.
How to validate the form in android easily
Most of in every single app we need to validate some form. Whether its login, register or some time contact us forms are there in the application. However, it is not hard but its replicated code we have written again and again. so here I would like to suggest one good library which is very helpful in order to achieve our goal and reduce boiler code.
So, Let's get started, which lib is this and how it will be integrated.
The first step first, Add dependency Gradle
implementation ‘com.github.dhaval2404:android-form-validation:1.0’
It is highly customizable and easy to use. This library will works with TextView, EditText, AppCompatEditText, TextInputEditText, TextInputLayout and CheckBox. This library is designed in such a way that it is easy to add support for new widgets and add new rules.
The android-form-validation configuration is created using the builder pattern.
Here are some general field and way to validate it.
private fun isValidForm(): Boolean {
return FormValidator.getInstance()
.addField(firstNameEt, NonEmptyRule(R.string.error_empty_first_name))
.addField(lastNameEt, NonEmptyRule(R.string.error_empty_last_name))
.addField(
emailEtLyt,
NonEmptyRule("Please enter Email"),
EmailRule(R.string.error_invalid_email)
)
.addField(
passwordEtLyt,
NonEmptyRule("Please enter Password"),
PasswordRule("Please provide strong Password")
)
.addField(
confirmPasswordEtLyt,
NonEmptyRule("Please enter Password"),
EqualRule(
confirmPasswordEt.text.toString(),
"Password and Confirm password must match"
)
)
.addField(
phoneNumberEt,
NonEmptyRule("Please enter Phone Number"),
LengthRule(10, "Please enter valid Phone Number")
)
.addField(
termsOfUseCB,
CheckedRule("Accept terms of use")
)
.setErrorListener {
// Require only for CheckBox with Toast or Custom View Only
for (error in it) {
if (error.view is CheckBox) {
(error.view as CheckBox).error = null
Toast.makeText(applicationContext, error.error, Toast.LENGTH_SHORT).show()
}
Log.e("Error", error.toString())
}
}
.validate()
}
Apart from that filed some times, we have to make sure terms and condition or other kinds of checkbox must be selected, so How to validate that using this amazing lib. The library provides an outbox solution for that. Check below methods for the same.
📐Supported Rules:
→ CheckedRule(Used with CheckBox only)
addField(termsOfUseCheckbox, CheckedRule(“Accept terms of use”))
→ EqualRule
// Combile 2 rules for Confirm Password Validation
// Check if Password is valid and Password match with Confirm Password
addField(confirmPasswordEditText,
PasswordRule(PasswordPattern.ALPHA_NUMERIC, "Please provide strong Password"),
EqualRule(passwordEditText.text.toString(), "Password not matching")
)// Combile 2 rules for Confirm Email Validation
// Check if Email is valid and Email match with Confirm Email
addField(emailEditText,
EmailRule("Please enter valid Email"),
EqualRule(confirmEmailEditText.text.toString(), "Email not matching")
)
→ LengthRule
addField(phoneNumberEditText, LengthRule(10, “Please enter valid Phone Number”))
→ MaxLengthRule
addField(messageEditText, MaxLengthRule(50, “Message should be less than 50 character long”))
→ MinLengthRule
addField(messageEditText, MinLengthRule(10, "Please enter message with at least 10 character"))// Check if Phone Number Length is between 10-13
addField(phoneNumberEditText,
MinLengthRule(10, "Please enter valid Phone Number"),
MaxLengthRule(13, "Please enter valid Phone Number"))
→ NumberRule
addField(priceEditText, NumberRule("Please enter valid Price"))
→ NonEmptyRule
addField(firstNameEditText, NonEmptyRule("Please enter First Name"))
→ PasswordRule
// Password can have any characters
addField(passwordEditText, PasswordRule("Please enter Password"))// Password should have alphabets and numeric character both
addField(passwordEditText, PasswordRule(PasswordPattern.ALPHA_NUMERIC, "Please provide strong Password"))
→ RegexRule
addField(usernameEditText, RegexRule(RegexRule.USERNAME_PATTERN, "Please enter valid Username"))addField(usernameEditText, RegexRule("^[a-zA-Z0-9_-]{3,16}", "Please enter valid Username"))
So this is the beauty of the library, Kindly let me know if you find any difficulties to integrate it. Write your concerns and suggestion in comments.
Thank you very for your time.