├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── alexfu │ │ └── formvalidatordemo │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── alexfu │ │ └── formvalidator │ │ ├── TextViewHelper.java │ │ ├── ValidationResult.java │ │ ├── ValidationTask.java │ │ ├── Validator.java │ │ ├── rules │ │ ├── EmailRule.java │ │ ├── MinLengthRule.java │ │ ├── RegexRule.java │ │ └── ValidationRule.java │ │ └── utils │ │ └── Patterns.java │ └── test │ └── java │ └── com │ └── alexfu │ └── formvalidator │ ├── EmailRuleTests.kt │ ├── MinLengthRuleTests.kt │ └── RegexRuleTests.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | local.properties 4 | .idea 5 | .DS_Store 6 | build 7 | captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## [2.0](https://github.com/alexfu/form-validator/tree/2.0) (2019-03-06) 4 | [Full Changelog](https://github.com/alexfu/form-validator/compare/1.1.0...2.0) 5 | 6 | **Merged pull requests:** 7 | 8 | - Release 2.0 [\#8](https://github.com/alexfu/form-validator/pull/8) ([alexfu](https://github.com/alexfu)) 9 | - Update to handle setting errors automatically [\#7](https://github.com/alexfu/form-validator/pull/7) ([alexfu](https://github.com/alexfu)) 10 | - Update build tools and everything else [\#6](https://github.com/alexfu/form-validator/pull/6) ([alexfu](https://github.com/alexfu)) 11 | 12 | ## [1.1.0](https://github.com/alexfu/form-validator/tree/1.1.0) (2017-04-26) 13 | [Full Changelog](https://github.com/alexfu/form-validator/compare/1.0...1.1.0) 14 | 15 | **Merged pull requests:** 16 | 17 | - Removable rules [\#5](https://github.com/alexfu/form-validator/pull/5) ([alexfu](https://github.com/alexfu)) 18 | 19 | ## [1.0](https://github.com/alexfu/form-validator/tree/1.0) (2017-03-16) 20 | [Full Changelog](https://github.com/alexfu/form-validator/compare/0.2...1.0) 21 | 22 | **Closed issues:** 23 | 24 | - Rx [\#2](https://github.com/alexfu/form-validator/issues/2) 25 | 26 | **Merged pull requests:** 27 | 28 | - Implements V1 API [\#4](https://github.com/alexfu/form-validator/pull/4) ([alexfu](https://github.com/alexfu)) 29 | 30 | ## [0.2](https://github.com/alexfu/form-validator/tree/0.2) (2016-12-24) 31 | [Full Changelog](https://github.com/alexfu/form-validator/compare/0.1...0.2) 32 | 33 | **Closed issues:** 34 | 35 | - Not Working [\#1](https://github.com/alexfu/form-validator/issues/1) 36 | 37 | **Merged pull requests:** 38 | 39 | - Extensible [\#3](https://github.com/alexfu/form-validator/pull/3) ([alexfu](https://github.com/alexfu)) 40 | 41 | ## [0.1](https://github.com/alexfu/form-validator/tree/0.1) (2016-08-12) 42 | 43 | 44 | \* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Alex Fu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # form-validator 2 | 3 | A simple, easy to use, no frills, form validator for Android. 4 | 5 | # Usage 6 | 7 | ```java 8 | EmailRule emailRule = new EmailRule("Invalid email address."); 9 | 10 | Validator validator = new Validator(); 11 | validate.setCallback(this); 12 | validator.addRule(myEditText, emailRule); // Add rules to your EditText 13 | validator.validate(); 14 | ``` 15 | 16 | If you want to have multiple rules... 17 | 18 | ```java 19 | EmailRule emailRule = new EmailRule("Invalid email address."); 20 | MinLengthRule minLengthRule = new MinLengthRule(5, "Must be at least 5 characters long.") 21 | 22 | Validator validator = new Validator(); 23 | validate.setCallback(this); 24 | validator.addRule(myEditText, emailRule, minLengthRule); // Add rules to your EditText 25 | validator.validate(); 26 | ``` 27 | 28 | Adding rules will also bind a `TextWatcher` to the given `EditText` and validate it on the fly. 29 | 30 | # Installation 31 | 32 | ``` 33 | allprojects { 34 | repositories { 35 | maven { url 'https://jitpack.io' } 36 | } 37 | } 38 | 39 | dependencies { 40 | compile 'com.github.alexfu:form-validator:2.0' 41 | } 42 | ``` 43 | 44 | # Rx 45 | 46 | Fancy an Rx API? Take a look at [form-validator-rx](https://github.com/alexfu/form-validator-rx). 47 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.11' 5 | ext.spek_version = '1.0.9' 6 | 7 | repositories { 8 | google() 9 | jcenter() 10 | } 11 | dependencies { 12 | classpath 'com.android.tools.build:gradle:3.2.1' 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | maven { url "http://repository.jetbrains.com/all" } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion "28.0.3" 6 | defaultConfig { 7 | applicationId "com.alexfu.formvalidatordemo" 8 | minSdkVersion 15 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation project(':library') 25 | } 26 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/alexfu/.android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /demo/src/main/java/com/alexfu/formvalidatordemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.alexfu.formvalidatordemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import android.widget.EditText; 10 | import android.widget.Toast; 11 | 12 | import com.alexfu.formvalidator.ValidationResult; 13 | import com.alexfu.formvalidator.Validator; 14 | import com.alexfu.formvalidator.rules.EmailRule; 15 | import com.alexfu.formvalidator.rules.MinLengthRule; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class MainActivity extends AppCompatActivity implements Validator.Callback { 21 | private Button validateButton; 22 | private EditText firstNameInput, lastNameInput, emailInput; 23 | private Validator validator = new Validator(); 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | setUpViews(); 30 | setUpValidator(); 31 | } 32 | 33 | private void setUpValidator() { 34 | validator.setCallback(this); 35 | validator.addRule(firstNameInput, new MinLengthRule(1, "First name cannot be empty.")); 36 | validator.addRule(lastNameInput, new MinLengthRule(1, "Last name cannot be empty.")); 37 | validator.addRule(emailInput, new EmailRule("Invalid email.")); 38 | 39 | // Form validation 40 | 41 | validateButton.setOnClickListener(new View.OnClickListener() { 42 | @Override public void onClick(View view) { 43 | validator.validate(); 44 | } 45 | }); 46 | } 47 | 48 | @Override public void onBeginFormValidation() { 49 | Log.d("MainActivity", "onBeginFormValidation"); 50 | } 51 | 52 | @Override public void onFieldValidated(@NonNull ValidationResult result) { 53 | Log.d("MainActivity", "onFieldValidated"); 54 | } 55 | 56 | @Override 57 | public void onSuccessValidation() { 58 | Log.d("MainActivity", "onSuccessValidation"); 59 | } 60 | 61 | @Override 62 | public void onFailedValidation(@NonNull List errors) { 63 | Log.d("MainActivity", "onFailedValidation"); 64 | } 65 | 66 | private void setUpViews() { 67 | firstNameInput = findViewById(R.id.first_name_input); 68 | lastNameInput = findViewById(R.id.last_name_input); 69 | emailInput = findViewById(R.id.email_input); 70 | validateButton = findViewById(R.id.button_validate); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 16 | 17 | 23 | 24 | 31 | 32 |