Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anderscheow/Validator
A tool to validate text inside TextInputLayout
https://github.com/anderscheow/Validator
android android-library edittext textinputedittext textinputlayout validation validator
Last synced: about 1 month ago
JSON representation
A tool to validate text inside TextInputLayout
- Host: GitHub
- URL: https://github.com/anderscheow/Validator
- Owner: anderscheow
- License: mit
- Created: 2017-11-17T10:14:54.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-10-10T14:12:09.000Z (over 3 years ago)
- Last Synced: 2024-11-06T11:04:17.999Z (2 months ago)
- Topics: android, android-library, edittext, textinputedittext, textinputlayout, validation, validator
- Language: Kotlin
- Homepage:
- Size: 378 KB
- Stars: 127
- Watchers: 7
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-android - Validator - An utilities class to validate text inside TextInputLayout. (Libraries / Utility)
- awesome-android - Validator - An utilities class to validate text inside TextInputLayout. (Libraries / Utility)
README
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Validator-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/6478) [![](https://jitpack.io/v/anderscheow/Validator.svg)](https://jitpack.io/#anderscheow/Validator)
[![codecov](https://codecov.io/gh/anderscheow/Validator/branch/master/graph/badge.svg)](https://codecov.io/gh/anderscheow/Validator/branch/develop)## Download
```groovy
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
* 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)
```kotlin
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
```kotlin
// 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
```kotlin
validator(applicationContext) {
/* Set your mode here, by default is CONTINUOUS */
mode = Mode.SINGLE
}
```| Single | Continuous |
| --- | --- |
| ![](https://media.giphy.com/media/3ohs7YJIZfbrC7txyU/giphy.gif) | ![](https://media.giphy.com/media/3ohs84PogwMOkUg0Jq/giphy.gif) |### Validate the input field
```kotlin
// 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) {
Log.d("MainActivity", values.toTypedArray().contentToString())
Toast.makeText(
applicationContext,
"Validate successfully",
Toast.LENGTH_LONG
).show()
}override fun onValidateFailed(errors: List) {
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](https://github.com/anderscheow/Validator/tree/master/library/src/test/java/io/github/anderscheow/validator)## 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](https://github.com/anderscheow/Validator/blob/master/LICENSE)