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:

// This example will check that whether user entered password has
// atleast one number, one spcial character, and one upper case.
var txtPassword = findViewById<EditText>(R.id.txtPassword)
txtPassword.validator()
.nonEmpty()
.atleastOneNumber()
.atleastOneSpecialCharacters()
.atleastOneUpperCase()
.addErrorCallback {
txtPassword.error = it
// it will contain the right message.
// For example, if edit text is empty,
// then 'it' will show "Can't be Empty" message
}
.check()

Create Your Own Custom Validation Checks

You can also add your own custom by extending BaseRule class.

class HelloRule : BaseRule
{
// add your validation logic in this method
override fun validate(text: String) : Boolean
{
// Apply your validation rule logic here
return text.contains("hello")
}
// Add your invalid check message here
override fun getErrorMessage() : String
{
return "Text should contain 'hello' keyword"
}
}

You can use this rule using Validator.addRule() method like this:

var myEditText = findViewById<EditText>(R.id.myEditText)
var myEditText.validator()
.addRule(HelloRule())
.addErrorCallback {
// In case of invalid, this method will be called.
// The 'it' will be "Text should contain 'hello' keyword" message.
myEditText.error = it
}
.check()

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

var txtUsername = findViewById<EditText>(R.id.txtUsername)
var txtEmail = findViewById<EditText>(R.id.txtEmail)
var txtMobileNum = findViewById<EditText>(R.id.txtMobileNum)
var txtPassword = findViewById<EditText>(R.id.txtPassword)
if (txtUsername.nonEmpty()
&& txtEmail.nonEmpty()
&& txtMobileNum.nonEmpty()
&& txtPassword.nonEmpty())
{
// All views are non-empty. You are safe to submit the data to server.
}
else
{
// Any one or more fields are empty. Show the error to the user
// to fill the whole form.
// You don't know which field is empty. So you might need to find it.
}

But, EasyValidation also provides you the collection extensions to make this easier.

RIGHT WAY

var txtUsername = findViewById<EditText>(R.id.txtUsername)
var txtEmail = findViewById<EditText>(R.id.txtEmail)
var txtMobileNum = findViewById<EditText>(R.id.txtMobileNum)
var txtPassword = findViewById<EditText>(R.id.txtPassword)
// Empty check
nonEmptyList(txtUsername, txtEmail, txtMobileNum, txtPassword) { view, msg ->
view.error = msg
// The view will contain the exact view which is empty
// The msg will contain the error message
}
// Min-length Check - all views should have atleast 5 characters
minLengthList(5, txtUsername, txtEmail, txtMobileNum, txtPassword) { view, msg ->
view.error = msg
// The view will contain the exact view which is less than 5 characters
// The msg will contain the error message
}

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.