Introduction
EasyValidation is a text validation library for Android developed in Kotlin. It supports text validation for String, EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner. It comes with lots of built-in rules for validation such as email, password, credit cards, special character validations and so on.
Quick String Validation
For example, you can validate any email string like this:
var myEmailStr = "john.doe@gmail.com"
var isValid = myEmailStr.validEmail() // isValid will be true or false
// Or you can also validate with an error callback method
myEmailStr.validEmail() {
// This method will be called when myEmailStr is not a valid email.
Toast.makeText(contex, it, Toast.LENGTH_SHORT).show()
}Text View Validations
These built-in rules can also be applied on text views like EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner like this.
var myEditText = findViewById<EditText>(R.id.myEditText)
var isValid = myEditText.nonEmpty() // Checks if edit text is empty or not
// Or with error callback method like this
myEditText.nonEmpty() {
// This method will be called when myEditText is empty.
myEditText.error = it
}There are around 30+ built-in rules in the core module library. You can check all these in Rules page.
Multiple Validation Checks
EasyValidation also supports multiple validation checks at same time using Validator class like this:
Create Your Own Custom Validation Checks
You can also add your own custom by extending BaseRule class.
You can use this rule using Validator.addRule() method like this:
Applying Rules on Multiple Views/Strings
For example, your app has a long text input form where you have got lots of input views. You might perform checks in EasyValidation like this.
WRONG WAY
But, EasyValidation also provides you the collection extensions to make this easier.
RIGHT WAY
You can apply all the built-in rules just by adding List suffix in the rule name. For example, nonEmpty becomes nonEmptyList, the validEmail becomes validEmailList and so on.
Last updated