Coder Social home page Coder Social logo

anderscheow / validator Goto Github PK

View Code? Open in Web Editor NEW
127.0 7.0 12.0 387 KB

A tool to validate text inside TextInputLayout

License: MIT License

Java 0.10% Kotlin 99.90%
edittext validation validator android android-library textinputlayout textinputedittext

validator's Introduction

Android Arsenal codecov

Download

dependencies {
  implementation 'com.github.anderscheow:validator:3.0.3'
}

Usage

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)

Beside from using the provided Rules, you can create your own Rule by extending Rule (Create as many as you want)

class CustomRule : Rule("Value doesn't match 'ABC'") {
 
    override fun validate(value: String?): Boolean {
        if (value == null) {
            throw NullPointerException()
        }
        return value == "ABC"
    }
}

Add it to your Activity class

// Username
// Input: [email protected]
val usernameInput = findViewById(R.id.layout_username)
val usernameValidation = validation(usernameInput) {
    conditions {
        +and(errorMessage = "Does not match 'And' condition") {
            +email(errorMessage = "")
            +endsWith(keyword = ".com", errorMessage = "")
        }
        +or(errorMessage = "Does not match 'Or' condition") {
            +minimumLength(minLength = 8, errorMessage = "")
            +contain(keyword = "hello", errorMessage = "")
        }
    }
}
 
// Password
// Input: 12345abc
val passwordInput = findViewById(R.id.layout_password)
val passwordValidation = validation(passwordInput) {
    rules {
        +notEmpty(errorMessage = "Input is empty")
        +minimumLength(minLength = 8, errorMessage = "Must have at least 8 characters")
    }
}

Mode

validator(applicationContext) {
    /* Set your mode here, by default is CONTINUOUS */
    mode = Mode.SINGLE
}
Single Continuous

Validate the input field

// Order of the values on the success callback follow the sequence of your Validation object
validator(applicationContext) {
    mode = Mode.SINGLE
    listener = 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)
}
  • Introduce Validator library

Testing

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

Contributions

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

License

Validator is released under the MIT License

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.