Awesome Open Source
Awesome Open Source

Android Arsenal Build Status codecov


dependencies {
  implementation 'com.github.anderscheow:validator:2.2.1'


Available rules

  • LengthRule
  • MaxRule
  • MinRule
  • NotEmptyRule
  • NotNullRule
  • RegexRule
  • AlphabetRule
  • AlphanumericRule
  • DigitsRule
  • EmailRule
  • PasswordRule
  • FutureRule
  • PastRule
  • CreditCardRule
  • ContainRule
  • NotContainRule
  • EqualRule
  • NotEqualRule
  • NotBlankRule
  • AllUpperCaseRule
  • AllLowerCaseRule
  • EndsWithRule
  • StartsWithRule
  • SymbolRule

Additional predefined rules to use in Validation or Condition

  • contain
  • notContain
  • notEqualTo
  • withinRange
  • minimumLength
  • maximumLength
  • email
  • alphanumericOnly
  • alphabetOnly
  • digitsOnly
  • symbolsOnly
  • allUppercase
  • allLowercase
  • startsWith
  • endsWith
  • withCreditCard
  • withPassword
  • notNull
  • notEmpty
  • notBlank
  • regex
  • future
  • past
  • 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: String?): 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(
val usernameValidation = Validation(usernameInput)
                    // You can also override the default error message
                    // Use either errorRes() or errorMessage()
                    // Note: errorRes() has higher priority
                    NotEmptyRule("Value is empty")
// Password
// Input: 12345abc
val passwordInput = findViewById(
val passwordValidation = Validation(passwordInput)


// And Condition
val usernameWithConditionValidation = Validation(usernameInput)
// Or Condition
val usernameWithConditionValidation = Validation(usernameInput)
// Both by using Predefined Rules
val usernameWithConditionValidation = new Validation(usernameInput)
                .matchAtLeastOneRule(listOf(MinRule(5), MaxRule(10)))


            /* Set your mode here, by default is CONTINUOUS */
Single Continuous

Validate the input field

// Order of the values on the success callback follow the sequence of your Validation object
            .setListener(object : Validator.OnValidateListener {
                    override fun onValidateSuccess(values: List<String>) {
                        Log.d("MainActivity", values.toTypedArray().contentToString())
                        Toast.makeText(applicationContext, "Validate successfully", Toast.LENGTH_LONG).show()

                    override fun onValidateFailed(errors: List<String>) {
                        Log.e("MainActivity", errors.toTypedArray().contentToString())
            .validate(usernameValidation, passwordValidation)



  • Added Validation.notNull, Validation.notEmpty, Validation.notBlank, Validation.regex, Validation.past and Validation.future
  • Can use TextInputLayout.validate or TextInputLayout.add instead of Validation(TextInputLayout) in Kotlin


  • Added AllUpperCaseRule, AllLowerCaseRule, StartsWithRule, EndsWithRule and SymbolRule
  • Changed validate(Any?) to validate(String?)


  • Added NotEmptyRule
  • Added error messages into Validator.OnValidateListener.onValidateFailed()


  • Updated Gradle and Kotlin version
  • Changed Android Support artifacts to AndroidX
  • Removed some install dependencies from README


  • Updated Gradle and Kotlin version
  • Changed Android Support artifacts to AndroidX
  • Removed some install dependencies from README


  • Updated to support Android SDK 28
  • Converted Android Support to AndroidX


  • Addressed some algorithm issues
  • Added more test cases


  • Removed Validation.and() and Validation.or() by encouraging user to use Condition
  • Removed listener parameter from Validator.validate() and required to assigned manually using Validator.setListener()
  • Added some predefined rules in Validation and Condition to simplify procedure on adding rules


  • Added Dokka to show Kotlin sources


  • Removed generic type from BaseRule as there's a limitation

  • validate() only accept "Any?" as parameter


  • For version lower than 1.2.0, upgrading to latest version may broke your code. Use it at your risk

  • Updated to Kotlin

  • Validation does support for Object parameter instead of just TextInputLayout (Refer to example below)

  • Set TextInputLayout.setErrorEnabled(false) on every Validate method called


  • Removed unwanted log


  • Fixed an issue where validate will success if last validation pass the rules in Mode.CONTINUOUS

  • RegexRule now is open class rather than abstract class


  • Input can be any object, previously restrict to String (Along with proper validation with different object)

  • Added more test cases to validate input


  • Updated ALPHA_NUMERIC_SYMBOLS regex on PasswordRule


  • Fixed bug where overloading the constructor with errorMessage or errorRes does not override the default value


  • Added ability to add conditions (And or Or)

  • Added mode of validation (Single or Continuous)

  • Added ability to override errorRes or errorMessage in constructor without overriding the methods


  • Added more rules, please check at 'Available Rules'


  • Fixed LengthRule wrong validation on maxValue


  • Added success and failed callback instead of just success callback

  • Success callback return list of EditText values (Order by sequence of Validation object(s))


  • Added some common rules (LengthRule, MinRule, MaxRule, RegexRule etc.)

  • Able to use String as error message


  • Introduce Validator library


I have added unit testing for Rules and Conditions, soon will provide test code on Validation and Validator, please check it out under Test code


Any contribution is more than welcome! You can contribute through pull requests and issues on GitHub.


Validator is released under the MIT License

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
android (6,472
kotlin (3,775
android-library (815
validation (329
validator (107
edittext (32