EasyValidation
A text and input validation library in Kotlin for Android.
? Installation
Add this in app's build.gradle
file:
implementation "com.wajahatkarim3.easyvalidation:easyvalidation-core:1.0.1"
⭐️ Features
- The Validator way validation support. - Details
- 30+ built-in validation rules like empty, email, credit cards, etc. - Rules List
- Extension methods for
String
,EditText
,TextView
,AutoCompleteTextView
,TextInputLayout
, andSpinner
. - Details - Multiple Validations and Checks - Details
- Collection Extension Methods for validations on multiple texts and views - Details
- Create your own custom rules - Details
❓ Quick Usage
For example, you can validate any email String
like this:
var myEmailStr = "[email protected]"
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()
}
These extension methods are also available for String
, EditText
, TextView
, AutoCompleteTextView
, TextInputLayout
, and Spinner
.
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.
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()