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:
1
var myEmailStr = "[email protected]"
2
var isValid = myEmailStr.validEmail() // isValid will be true or false
3
4
// Or you can also validate with an error callback method
5
myEmailStr.validEmail() {
6
// This method will be called when myEmailStr is not a valid email.
7
Toast.makeText(contex, it, Toast.LENGTH_SHORT).show()
8
}
Copied!

Text View Validations

These built-in rules can also be applied on text views like EditText, TextView, AutoCompleteTextView, TextInputLayout, and Spinner like this.
1
var myEditText = findViewById<EditText>(R.id.myEditText)
2
var isValid = myEditText.nonEmpty() // Checks if edit text is empty or not
3
4
// Or with error callback method like this
5
myEditText.nonEmpty() {
6
// This method will be called when myEditText is empty.
7
myEditText.error = it
8
}
Copied!
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:
1
// This example will check that whether user entered password has
2
// atleast one number, one spcial character, and one upper case.
3
4
var txtPassword = findViewById<EditText>(R.id.txtPassword)
5
txtPassword.validator()
6
.nonEmpty()
7
.atleastOneNumber()
8
.atleastOneSpecialCharacters()
9
.atleastOneUpperCase()
10
.addErrorCallback {
11
txtPassword.error = it
12
// it will contain the right message.
13
// For example, if edit text is empty,
14
// then 'it' will show "Can't be Empty" message
15
}
16
.check()
Copied!

Create Your Own Custom Validation Checks

You can also add your own custom by extending BaseRule class.
1
class HelloRule : BaseRule
2
{
3
// add your validation logic in this method
4
override fun validate(text: String) : Boolean
5
{
6
// Apply your validation rule logic here
7
return text.contains("hello")
8
}
9
10
// Add your invalid check message here
11
override fun getErrorMessage() : String
12
{
13
return "Text should contain 'hello' keyword"
14
}
15
}
Copied!
You can use this rule using Validator.addRule() method like this:
1
var myEditText = findViewById<EditText>(R.id.myEditText)
2
var myEditText.validator()
3
.addRule(HelloRule())
4
.addErrorCallback {
5
// In case of invalid, this method will be called.
6
// The 'it' will be "Text should contain 'hello' keyword" message.
7
myEditText.error = it
8
}
9
.check()
Copied!

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

1
var txtUsername = findViewById<EditText>(R.id.txtUsername)
2
var txtEmail = findViewById<EditText>(R.id.txtEmail)
3
var txtMobileNum = findViewById<EditText>(R.id.txtMobileNum)
4
var txtPassword = findViewById<EditText>(R.id.txtPassword)
5
6
if (txtUsername.nonEmpty()
7
&& txtEmail.nonEmpty()
8
&& txtMobileNum.nonEmpty()
9
&& txtPassword.nonEmpty())
10
{
11
// All views are non-empty. You are safe to submit the data to server.
12
13
}
14
else
15
{
16
// Any one or more fields are empty. Show the error to the user
17
// to fill the whole form.
18
// You don't know which field is empty. So you might need to find it.
19
}
Copied!
But, EasyValidation also provides you the collection extensions to make this easier.

RIGHT WAY

1
var txtUsername = findViewById<EditText>(R.id.txtUsername)
2
var txtEmail = findViewById<EditText>(R.id.txtEmail)
3
var txtMobileNum = findViewById<EditText>(R.id.txtMobileNum)
4
var txtPassword = findViewById<EditText>(R.id.txtPassword)
5
6
// Empty check
7
nonEmptyList(txtUsername, txtEmail, txtMobileNum, txtPassword) { view, msg ->
8
view.error = msg
9
// The view will contain the exact view which is empty
10
// The msg will contain the error message
11
}
12
13
// Min-length Check - all views should have atleast 5 characters
14
minLengthList(5, txtUsername, txtEmail, txtMobileNum, txtPassword) { view, msg ->
15
view.error = msg
16
// The view will contain the exact view which is less than 5 characters
17
// The msg will contain the error message
18
}
Copied!
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 modified 3yr ago