Validator
A tool to validate text inside TextInputLayout.
Download
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'io.github.anderscheow:validator:2.0.0'
}
Usage
Available rules
- LengthRule
- MaxRule
- MinRule
- NotEmptyRule
- NotNullRule
- RegexRule
- AlphabetRule
- AlphanumericRule
- DigitsRule
- EmailRule
- PasswordRule
- FutureRule
- PastRule
- CreditCardRule
- ContainRule
- NotContainRule
- EqualRule
- NotEqualRule
Additional predefined rules to use in Validation or Condition
- contain
- notContain
- notEqualTo
- withinRange
- minimumLength
- maximumLength
- alphanumericOnly
- alphabetOnly
- digitsOnly
- symbolsOnly
- allUppercase
- allLowercase
- startsWith
- endsWith
- withCreditCard
- withPassword
- matchAtLeastOneRule (Only for Validation)
- matchAllRules (Only for Validation)
You can create your own Predefined Rules
// For Validation
fun Validation.customPredefinedRule(keyword: String, ignoreCase: Boolean = false): Validation {
baseRules.add(ContainRule(keyword, ignoreCase))
return this
}
// For Condition
fun Condition.customPredefinedRule(keyword: String, ignoreCase: Boolean = false): Condition {
baseRules.add(ContainRule(keyword, ignoreCase))
return this
}
Beside from using the provided Rules, you can create your own Rule by extending BaseRule (Create as many as you want)
class CustomRule : BaseRule {
override fun validate(value: Any?): Boolean {
if (value == null) {
throw NullPointerException()
}
return value == "ABC"
}
// You can use this to return error message
override fun getErrorMessage(): String {
return "Value doesn't match 'ABC'"
}
// or use this to return error message as StringRes
override fun getErrorRes(): Int {
return R.string.error_not_match
}
}
Add it to your Activity class
// Username
// Input: [email protected]
val usernameInput = findViewById(R.id.layout_username)
val usernameValidation = Validation(usernameInput)
.addRule(
// You can also override the default error message
NotEmptyRule(R.string.error_not_empty)
// Use either errorRes() or errorMessage()
// Note: errorRes() has higher priority
NotEmptyRule("Value is empty")
)
.addRule(CustomRule())
// Password
// Input: 12345abc
val passwordInput = findViewById(R.id.layout_password)
val passwordValidation = Validation(passwordInput)
.addRule(NotEmptyRule())
.withPassword(PasswordRegex.ALPHA_NUMERIC)
.minimumLength(8)
Condition
// And Condition
val usernameWithConditionValidation = Validation(usernameInput)
.add(And().add(EmailRule()))
// Or Condition
val usernameWithConditionValidation = Validation(usernameInput)
.add(Or().add(MinRule(5)).add(MaxRule(10)))
// Both by using Predefined Rules
val usernameWithConditionValidation = new Validation(usernameInput)
.matchAllRules(listOf(EmailRule()))
.matchAtLeastOneRule(listOf(MinRule(5), MaxRule(10)))
Mode
Validator.with(applicationContext)
/* Set your mode here, by default is CONTINUOUS */
.setMode(Mode.CONTINUOUS));
Single | Continuous |
---|---|
Validate the input field
// Order of the values on the success callback follow the sequence of your Validation object
Validator.with(applicationContext)
.setMode(Mode.CONTINUOUS)
.setListener(object : Validator.OnValidateListener {
override fun onValidateSuccess(values: List<String>) {
Log.d("MainActivity", Arrays.toString(values.toTypedArray()))
Toast.makeText(applicationContext, "Validate successfully", Toast.LENGTH_LONG).show()
}
override fun onValidateFailed() {
Toast.makeText(applicationContext, "Validate failed", Toast.LENGTH_LONG).show()
}
})
.validate(usernameValidation, passwordValidation)
New Feature: Validation does support for Object (Java)
and Any (Kotlin)
parameter
val usernameWithConditionValidation = Validation("[email protected]")
.add(And().add(EmailRule()))