├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── .gitignore ├── AndroidManifest.xml ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml ├── proguard-project.txt ├── project.properties ├── res │ ├── values-ko │ │ └── validator_strings.xml │ ├── values-v11 │ │ └── themes.xml │ └── values │ │ ├── themes.xml │ │ └── validator_strings.xml ├── src │ └── com │ │ └── throrinstudio │ │ └── android │ │ └── common │ │ └── libs │ │ └── validator │ │ ├── AbstractValidate.java │ │ ├── AbstractValidator.java │ │ ├── Form.java │ │ ├── Validate.java │ │ ├── ValidatorException.java │ │ ├── interfaces │ │ └── CustomErrorNotification.java │ │ ├── validate │ │ ├── ConfirmValidate.java │ │ └── OrTwoRequiredValidate.java │ │ └── validator │ │ ├── AlnumValidator.java │ │ ├── EmailValidator.java │ │ ├── HexValidator.java │ │ ├── IPAddressValidator.java │ │ ├── MockValidator.java │ │ ├── NotEmptyValidator.java │ │ ├── NumericValidator.java │ │ ├── PhoneValidator.java │ │ ├── RangeValidator.java │ │ ├── RegExpValidator.java │ │ └── UrlValidator.java └── test │ └── com │ └── throrinstudio │ └── android │ └── common │ └── libs │ └── validator │ ├── FormTest.java │ ├── RobolectricGradleTestRunner.java │ ├── ValidateText.java │ ├── validate │ ├── ConfirmValidateTest.java │ └── OrTwoRequiredValidateTest.java │ └── validator │ ├── AlnumValidatorTest.java │ ├── EmailValidatorTest.java │ ├── HexValidatorTest.java │ ├── NotEmptyValidatorTest.java │ ├── PhoneValidatorTest.java │ ├── RegExpValidatorTest.java │ └── UrlValidatorTest.java ├── others └── ic_launcher-web.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | library/bin/ 13 | library/gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | library/local.properties 17 | library/project.properties 18 | 19 | # Eclipse project files 20 | library/.classpath 21 | library/.project 22 | library/.settings 23 | 24 | # IntelliJ IDEA project files 25 | library/.idea 26 | *.iml 27 | *.eml 28 | library/classes 29 | 30 | # Command line 31 | library/build.xml 32 | library/proguard-project.txt 33 | 34 | # Mac specific 35 | .DS_Store 36 | 37 | # And specific 38 | library/ant.properties 39 | 40 | library/target 41 | library/build 42 | 43 | # Jetbrains 44 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Android-Validator 2 | ================= 3 | 4 | Form Validator Library for Android 5 | 6 | [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=throrin19&url=https://github.com/throrin19/Android-Validator/&title=Android Validator&language=Java&tags=github&category=software) 7 | 8 | Presentation 9 | ------------ 10 | 11 | Form Validator Library for Android is based on [Zend_Validator](http://framework.zend.com/manual/1.12/en/zend.validate.introduction.html, "Title") coded in PHP. This library is intended to simplify and streamline the code to validate a form Android. For the moment, the form can just handle the **EditText**. Other elements will emerge in future versions. 12 | 13 | ### License 14 | 15 | * [Apache Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) 16 | 17 | Requirement 18 | ----------- 19 | 20 | + Android 2.2+ 21 | 22 | Organization 23 | ------------ 24 | 25 | + **demo** application demo folder (coming soon) 26 | + **library** library project directory 27 | + **others** Others Resources directory (icon, sreenshots, ...) 28 | + **library/tests** Unit Test Directory (coming soon) 29 | 30 | Import in your Project 31 | ---------------------- 32 | 33 | ### Graddle Project 34 | 35 | Comming Soon 36 | 37 | ### Maven Project 38 | 39 | Comming Soon 40 | 41 | Testing 42 | --- 43 | 44 | In order to execute tests: 45 | ``` java 46 | gradle localTest 47 | ``` 48 | Results are available in app/build/reports/tests/index.html 49 | 50 | Use 51 | --- 52 | 53 | Form Validator Library is composed of 3 members : 54 | - **Form :** Contains all beings validates to treat. This is the Form that manages the display of error messages in the various elements. 55 | - **Validate :** Contains all the validators to be treated for a given element. 56 | - **Validator :** Can define a validation rule. 57 | 58 | ### Validator 59 | 60 | The validator is basic class for this library. It contains specific validation rules. To instanciate validator, you just have to do this (EmailValidator for example): 61 | 62 | ``` java 63 | new EmailValidator(context); 64 | ``` 65 | 66 | For some validators, functions can change the validation rules. The validator currently contains three basic validation rules: 67 | + **EmailValidator** : Ensures that the field does contain a email address. You can also define a regex to check for a particular domain name with the function `setDomainName(DomainRegexp)`. Example for **gmail.com** domain : `setDomainName("gmail\\.com")`. 68 | + **NotEmptyValidator** : Ensures that the field is not empty. 69 | + **UrlValidator** : Ensures that the field is a valid url. 70 | + **AlnumValidator** : Ensure that the feld has Alnum characters. 71 | + **HexValidator** : Ensure that the field has Hex characters. 72 | + **RegExpPattern** : Ensure that the field does match setted Pattern. 73 | + **PhoneValidator** : Ensure that the field is a valid phone number. 74 | + **RangeValidator** : Validates whether the given value is bettween a range of values 75 | + **NumericValidator** : Checks whether the field contains only numeric digits 76 | + **IPAddressValidator** : Checks whether the field contains a valid IP Address 77 | + **MockValidator** : Used by dev as a placeholder while testing validation 78 | + **Custom Validator** : You can create your own Validator. To do this, you can just create class extends AbstractValidator : 79 | 80 | ``` java 81 | public class CustomValidator extends AbstractValidator 82 | { 83 | private int mErrorMessage = R.string.validator_custom; // Your custom error message 84 | 85 | public CustomValidator(Context c) { 86 | super(c); 87 | } 88 | 89 | @Override 90 | public boolean isValid(Object value) { 91 | // Your validation Test is here. 92 | // Retour true if it's correct, false if it's incorrect 93 | return true; 94 | } 95 | 96 | @Override 97 | public String getMessage() { 98 | return mContext.getString(mErrorMessage); 99 | } 100 | } 101 | ``` 102 | 103 | ### Validate 104 | 105 | The pure Validate class is a FIFO validator. It's test a list of AbstractValidator for specific EditText. For some special cases, Validate is not enough. This is why there are specific validates. This is why there are two individuals with a Validate operation different from that base : 106 | + **ConfirmValidate** : Can check whether a value is identical between the two fields. Can be used to confirm the entry of a password. 107 | + **OrTwoRequiredValidate** : If one of the two target fields has a value, then it is valid. Can be used if a user must give his phone **or** fax. 108 | + **Validate** : The base Validate. It creates his validators stack. 109 | 110 | Basicly, Validate can only handle a single EditText. If you want to manage several at the same time, see if **ConfirmValidate ** or **OrTwoRequiredValidate ** match your problem. If this is not the case, you may need to create your own Validate. To instantiate a Validate, you just have to do this: 111 | ``` java 112 | Validate emailField = new Validate(email); 113 | ``` 114 | 115 | And to add Validator stack, for example to add a required Email field, you have to do this: 116 | ``` java 117 | emailField.addValidator(new NotEmptyValidator(mContext)); 118 | emailField.addValidator(new EmailValidator(mContext)); 119 | ``` 120 | 121 | ### Form 122 | 123 | The Form class is the class teacher of the whole Library. It is this which manages the processing of each Validate, Validator and displays the error on the EditText automatically. The Form class stores a Validate stack and then you just have to run the validation with the `validate()` function. 124 | To instanciate Form and add Validates, you have to do this : 125 | ``` java 126 | Form mForm = new Form(); 127 | mForm.addValidates(emailField); 128 | mForm.addValidates(confirmFields); 129 | mForm.addValidates(urlField); 130 | 131 | // ... 132 | 133 | // Launch Validation 134 | if(mForm.validate()){ 135 | // success statement 136 | }else{ 137 | // error statement like toast, crouton, ... 138 | } 139 | ``` 140 | 141 | You can close the error using the form. Can close all errors or just one : 142 | 143 | ```java 144 | // close one error 145 | mForm.closeError(emailField); 146 | // close all errors 147 | mForm.closeAllErrors() 148 | ``` 149 | 150 | ## Contrbute 151 | 152 | + Fork the repo 153 | + create a branch 154 | ``` 155 | git checkout -b my_branch 156 | ``` 157 | + Add your changes 158 | + Commit your changes: 159 | ``` 160 | git commit -am "Added some awesome stuff" 161 | ``` 162 | + Push your branch: 163 | ``` 164 | git push origin my_branch 165 | ``` 166 | + Make a pull request to `development` branch 167 | 168 | ## Changelog 169 | 170 | + **0.1** : Create library 171 | + **0.2** : Add ConfirmValidate and OrTwoRequiredValidate 172 | + **0.3** : Extends EmailValidator with `setDomainName()` 173 | + **0.4** : Replace Validator by AbstractValidator 174 | + **0.5** : Fix bug in UrlValidator 175 | + **1.0** : Add AlnumValidator, HexValidator, RegExpValidator, ValidatorException. Fix Validate class. 176 | + **1.1** : Go to Android 2.2 for android.util.Patterns. Add PhoneValidator. Edit UrlValidator. 177 | + **1.5** : Project reorganization, graddle and maven support 178 | + **1.6.0** : Fix bugs, optimize code, add contributions 179 | + **1.7.0** : Refactoring/Cleaning, add UnitTest 180 | + **1.8.0**: Optimize code, add new validators 181 | + **1.9.0** : Add validators, add closeError, closeAllErrors, translate into Korean, set the error message by resId or by specifying a string. 182 | + **1.10.0** : enable gradle builds of the .aar file, allow use of a custom error drawable, define custom error behavior. 183 | 184 | ## Contributors 185 | 186 | + [piobab](https://github.com/piobab) 187 | + [andreamaglie](https://github.com/andreamaglie) 188 | + [amondnet](https://github.com/amondnet) 189 | + [Leandros](https://github.com/Leandros) 190 | + [veelck](https://github.com/veelck) 191 | + [2bard](https://github.com/2bard) 192 | + [Exikle](https://github.com/Exikle) 193 | + [LiorZ](https://github.com/LiorZ) 194 | + [paddyzab](https://github.com/paddyzab) 195 | + [sys1yagi](https://github.com/sys1yagi) 196 | + [rogerg9999](https://github.com/rogerg9999) 197 | + [atermenji](https://github.com/atermenji) 198 | + [justincpollard](https://github.com/justincpollard) -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | // hack to fix jitpack building issue 7 | // related: https://code.google.com/p/android/issues/detail?id=192875 8 | classpath 'org.jacoco:org.jacoco.core:0.7.4.201502262128' 9 | } 10 | } 11 | 12 | 13 | /** 14 | * Task to generate a gradle wrapper. 15 | */ 16 | task wrapper(type: Wrapper) { 17 | gradleVersion = '2.9' 18 | } 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/throrin19/Android-Validator/ab6c0ef6d94c7a6c5ce48002676819e9b5fe9ea0/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 24 16:21:11 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # generated files 12 | bin/ 13 | gen/ 14 | 15 | # Local configuration file (sdk path, etc) 16 | local.properties 17 | project.properties 18 | 19 | 20 | # Eclipse project files 21 | .classpath 22 | .project 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Intellij project files 28 | *.iml 29 | *.ipr 30 | *.iws 31 | .idea/* 32 | 33 | #Gradle 34 | .gradle/* 35 | -------------------------------------------------------------------------------- /library/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | mavenCentral() 5 | mavenLocal() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:1.5.0' 10 | } 11 | } 12 | 13 | apply plugin: 'com.android.library' 14 | 15 | repositories { 16 | jcenter() 17 | maven { 18 | url "https://oss.sonatype.org/content/repositories/snapshots" 19 | } 20 | } 21 | 22 | sourceSets { 23 | testLocal { 24 | java.srcDir file('test') 25 | resources.srcDir file('test') 26 | } 27 | } 28 | 29 | android { 30 | compileSdkVersion 19 31 | buildToolsVersion "19.1.0" 32 | 33 | defaultConfig { 34 | minSdkVersion 14 35 | targetSdkVersion 19 36 | } 37 | 38 | sourceSets { 39 | main { 40 | manifest.srcFile 'AndroidManifest.xml' 41 | java.srcDirs = ['src'] 42 | resources.srcDirs = ['src'] 43 | renderscript.srcDirs = ['src'] 44 | res.srcDirs = ['res'] 45 | assets.srcDirs = ['assets'] 46 | } 47 | } 48 | 49 | // tell Android studio that the instrumentTest source set is located in the unit test source set 50 | sourceSets { 51 | instrumentTest.setRoot('test') 52 | } 53 | } 54 | 55 | dependencies { 56 | compile 'com.android.support:support-v4:19.0.+' 57 | 58 | // Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well 59 | testLocalCompile files("$project.buildDir/classes/release") 60 | testLocalCompile 'junit:junit:4.+' 61 | testLocalCompile 'com.android.support:support-v4:19.0.+' 62 | testLocalCompile 'org.robolectric:robolectric:2.+' 63 | testLocalCompile 'com.google.android:android:4.1.1.4' 64 | 65 | // Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest 66 | // which is Android Studio's test task 67 | instrumentTestCompile 'junit:junit:4.+' 68 | instrumentTestCompile 'com.android.support:support-v4:19.0.+' 69 | instrumentTestCompile 'org.robolectric:robolectric:2.+' 70 | instrumentTestCompile 'com.google.android:android:4.1.1.4' 71 | 72 | } 73 | 74 | task localTest(type: Test, dependsOn: assemble) { 75 | testClassesDir = sourceSets.testLocal.output.classesDir 76 | 77 | android.sourceSets.main.java.srcDirs.each { dir -> 78 | def buildDir = dir.getAbsolutePath().split('/') 79 | buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/') 80 | 81 | sourceSets.testLocal.compileClasspath += files(buildDir) 82 | sourceSets.testLocal.runtimeClasspath += files(buildDir) 83 | } 84 | 85 | classpath = sourceSets.testLocal.runtimeClasspath 86 | } 87 | -------------------------------------------------------------------------------- /library/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/throrin19/Android-Validator/ab6c0ef6d94c7a6c5ce48002676819e9b5fe9ea0/library/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /library/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 23 18:12:10 PDT 2014 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip 7 | -------------------------------------------------------------------------------- /library/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /library/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | android-validator 6 | android-validator 7 | 1.0 8 | apklib 9 | 10 | Android-Validator 11 | 12 | 13 | UTF-8 14 | 15 | 16 | 3.8.0 17 | 2.3.2 18 | 16 19 | 20 | 21 | 4.1.1.4 22 | 23 | 24 | 25 | 26 | com.google.android 27 | android 28 | provided 29 | ${android.version} 30 | 31 | 32 | 33 | 34 | src 35 | 36 | 37 | 38 | com.jayway.maven.plugins.android.generation2 39 | android-maven-plugin 40 | ${android-maven-plugin.version} 41 | true 42 | 43 | 44 | ${api.platform} 45 | 46 | 47 | 48 | true 49 | 50 | 51 | 52 | 53 | maven-compiler-plugin 54 | ${maven-compiler-plugin.version} 55 | 56 | 1.6 57 | 1.6 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /library/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /library/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | proguard.config=${sdk.dir}\\tools\\proguard\\proguard-android-optimize.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-18 15 | android.library=true 16 | -------------------------------------------------------------------------------- /library/res/values-ko/validator_strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 이메일 형식이 올바르지 않습니다. 4 | You must complete this field. 5 | URL 형식이 올바르지 않습니다. 6 | 입력값이 일치하지 않습니다. 7 | The value has not only alnum characters 8 | The value has not only hexadecimal digit characters 9 | The value does not match against pattern. 10 | 전화번호 형식이 올바르지 않습니다. 11 | -------------------------------------------------------------------------------- /library/res/values-v11/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /library/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /library/res/values/validator_strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | The e-mail address is not in a valid format. 4 | You must complete this field. 5 | The URL entered is incorrect. 6 | The confirm field is not valid. 7 | The value has not only alnum characters. 8 | The value has not only hexadecimal digit characters. 9 | The value does not match against pattern. 10 | Phone number is not in a valid format. 11 | The value is not between the required range. 12 | The entered date is not valid. 13 | The ip address is not valid. 14 | Inputed text contains something that isn\'t numeric. 15 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/AbstractValidate.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | public abstract class AbstractValidate { 4 | 5 | /** 6 | * Validate source. 7 | * 8 | * @return true if all validators are valid, otherwise false 9 | */ 10 | public abstract boolean isValid(); 11 | } 12 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/AbstractValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | 6 | /** 7 | * Class for creating new Validators 8 | * 9 | * @author throrin19 10 | */ 11 | public abstract class AbstractValidator { 12 | 13 | private Context mContext; 14 | 15 | private int mErrorMessageRes; 16 | 17 | private String mErrorMessageString; 18 | 19 | private Drawable mErrorDrawable; 20 | 21 | public AbstractValidator(Context c, int errorMessageRes) { 22 | mContext = c; 23 | mErrorMessageRes = errorMessageRes; 24 | mErrorMessageString = mContext.getString(mErrorMessageRes); 25 | } 26 | 27 | public AbstractValidator(Context c, String errorMessageString) { 28 | mContext = c; 29 | mErrorMessageString = errorMessageString; 30 | } 31 | 32 | public AbstractValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 33 | mContext = c; 34 | mErrorMessageRes = errorMessageRes; 35 | mErrorMessageString = mContext.getString(mErrorMessageRes); 36 | mErrorDrawable = errorDrawable; 37 | } 38 | 39 | /** 40 | * Can check if the value passed in parameter is valid or not. 41 | * 42 | * @param value 43 | * {@link String} : the value to validate 44 | * @return boolean : true if valid, false otherwise. 45 | */ 46 | public abstract boolean isValid(String value) 47 | throws ValidatorException; 48 | 49 | /** 50 | * Used to retrieve the error message corresponding to the validator. 51 | * 52 | * @return String : the error message 53 | */ 54 | public String getMessage() { 55 | return mErrorMessageString; 56 | } 57 | 58 | /** 59 | * Used to retrieve the error drawable to display on an error. 60 | * 61 | * @return Drawable : the error drawable 62 | */ 63 | public Drawable getErrorDrawable() { 64 | return mErrorDrawable; 65 | } 66 | 67 | /** 68 | * Sets the Context of the validator. Useful if we want to switch Context after a Configuration Change 69 | */ 70 | public void setContext(Context c) { 71 | this.mContext = c; 72 | } 73 | 74 | /** 75 | * Gets the textview's context 76 | * 77 | * @return Context 78 | */ 79 | public Context getContext() { 80 | return mContext; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/Form.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import android.widget.TextView; 7 | 8 | /** 9 | * Form Validation Class 10 | *

11 | * Immediately, only works with EditText 12 | * 13 | * @author throrin19 14 | * @version 1.0 15 | */ 16 | public class Form { 17 | 18 | private List mValidates = new ArrayList(); 19 | 20 | /** 21 | * Function adding Validates to our form 22 | * 23 | * @param validate 24 | * {@link AbstractValidate} Validate to add 25 | */ 26 | public void addValidates(AbstractValidate validate) { 27 | mValidates.add(validate); 28 | } 29 | 30 | /** 31 | * Function removing Validates from our form 32 | * 33 | * @param validate 34 | * {@link AbstractValidate} Validate to remove 35 | * @return validate that was removed from the form 36 | */ 37 | public boolean removeValidates(AbstractValidate validate) { 38 | if (mValidates != null && !mValidates.isEmpty()) { 39 | return mValidates.remove(validate); 40 | } 41 | return false; 42 | } 43 | 44 | /** 45 | * Called to validate our form. 46 | * If an error is found, it will be displayed in the corresponding field. 47 | * 48 | * @return boolean true if the form is valid, otherwise false 49 | */ 50 | public boolean validate() { 51 | boolean formValid = true; 52 | for (AbstractValidate validate : mValidates) { 53 | formValid = formValid & validate.isValid(); // Use & in order to evaluate both side of the operation. 54 | } 55 | return formValid; 56 | } 57 | 58 | /** 59 | * Closes the error popup of the text view. 60 | * A little useless due to ability to just call source.setError(null), but added anyways 61 | * 62 | * @param sourceTextView 63 | * @author Dixon D'Cunha (Exikle) 64 | */ 65 | public void closeError(TextView sourceTextView) { 66 | for (AbstractValidate av : mValidates) { 67 | Validate v = (Validate) av; 68 | if (v.getSource().equals(sourceTextView)) 69 | v.getSource().setError(null); 70 | } 71 | } 72 | 73 | /** 74 | * Closes all error pop ups that were created by validator 75 | * 76 | * @author Dixon D'Cunha (Exikle) 77 | */ 78 | public void closeAllErrors() { 79 | for (AbstractValidate av : mValidates) { 80 | Validate v = (Validate) av; 81 | v.getSource().setError(null); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/Validate.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import android.graphics.drawable.Drawable; 7 | import android.widget.TextView; 8 | 9 | import com.throrinstudio.android.common.libs.validator.interfaces.CustomErrorNotification; 10 | 11 | public class Validate extends AbstractValidate { 12 | 13 | private List mValidators = new ArrayList(); 14 | private TextView mSourceView; 15 | private CustomErrorNotification mCustomErrorNotification; 16 | 17 | public Validate(TextView sourceView) { 18 | mSourceView = sourceView; 19 | } 20 | 21 | public Validate(TextView sourceView, CustomErrorNotification customErrorNotification) { 22 | mSourceView = sourceView; 23 | mCustomErrorNotification = customErrorNotification; 24 | } 25 | 26 | /** 27 | * Add a new validator for fields attached 28 | * 29 | * @param validator {@link AbstractValidator} : The validator to attach 30 | */ 31 | public void addValidator(AbstractValidator validator) { 32 | mValidators.add(validator); 33 | } 34 | 35 | public boolean isValid() { 36 | for (AbstractValidator validator : mValidators) { 37 | try { 38 | if (!validator.isValid(mSourceView.getText().toString())) { 39 | if(mCustomErrorNotification != null) { 40 | mCustomErrorNotification.onInvalid(this); 41 | } else { 42 | setSourceViewError(validator.getMessage(), validator.getErrorDrawable()); 43 | } 44 | return false; 45 | } else { 46 | if(mCustomErrorNotification != null) mCustomErrorNotification.onValid(this); 47 | } 48 | } catch (ValidatorException e) { 49 | e.printStackTrace(); 50 | if(mCustomErrorNotification != null) { 51 | mCustomErrorNotification.onInvalid(this); 52 | } else { 53 | setSourceViewError(e.getMessage(), validator.getErrorDrawable()); 54 | } 55 | return false; 56 | } 57 | } 58 | mSourceView.setError(null); 59 | return true; 60 | } 61 | 62 | public TextView getSource() { 63 | return mSourceView; 64 | } 65 | 66 | public List getValidators() { 67 | return mValidators; 68 | } 69 | 70 | /** 71 | * Sets error on {@link #mSourceView}. 72 | * 73 | * @param errorMessage String : the error message 74 | * @param errorDrawable Drawable : the drawable to display 75 | */ 76 | private void setSourceViewError(String errorMessage, Drawable errorDrawable) { 77 | if(errorDrawable != null) { 78 | mSourceView.setError(errorMessage, errorDrawable); 79 | } else { 80 | mSourceView.setError(errorMessage); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/ValidatorException.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | public class ValidatorException extends java.lang.Exception { 4 | private static final long serialVersionUID = 1L; 5 | 6 | public ValidatorException() { 7 | super(); 8 | } 9 | 10 | public ValidatorException(String detailMessage, Throwable throwable) { 11 | super(detailMessage, throwable); 12 | } 13 | 14 | public ValidatorException(String detailMessage) { 15 | super(detailMessage); 16 | } 17 | 18 | public ValidatorException(Throwable throwable) { 19 | super(throwable); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/interfaces/CustomErrorNotification.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.interfaces; 2 | 3 | import com.throrinstudio.android.common.libs.validator.Validate; 4 | 5 | /** 6 | * Implemented to perform custom error actions rather 7 | * than using {@link android.widget.TextView#setError(CharSequence)} 8 | */ 9 | public interface CustomErrorNotification { 10 | /** 11 | * Called when an error has been detected. 12 | * 13 | * @param validate The specific {@link com.throrinstudio.android.common.libs.validator.Validate} 14 | * that is under examination. 15 | */ 16 | public void onInvalid(Validate validate); 17 | 18 | /** 19 | * Called when a field is confirmed to be valid. 20 | * 21 | * @param validate The specific {@link com.throrinstudio.android.common.libs.validator.Validate} 22 | * that is under examination. 23 | */ 24 | public void onValid(Validate validate); 25 | } 26 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validate/ConfirmValidate.java: -------------------------------------------------------------------------------- 1 | 2 | package com.throrinstudio.android.common.libs.validator.validate; 3 | 4 | import android.content.Context; 5 | import android.text.TextUtils; 6 | import android.widget.TextView; 7 | 8 | import com.throrinstudio.android.common.libs.validator.AbstractValidate; 9 | import com.throrinstudio.android.common.libs.validator.R; 10 | 11 | public class ConfirmValidate extends AbstractValidate { 12 | 13 | private static final int CONFIRM_ERROR_MESSAGE = R.string.validator_confirm; 14 | private TextView mFirstField; 15 | private TextView mSecondField; 16 | private Context mContext; 17 | private TextView mSourceView; 18 | private final int mErrorMessage; 19 | 20 | public ConfirmValidate(TextView field1, TextView field2) { 21 | mFirstField = field1; 22 | mSecondField = field2; 23 | mSourceView = mSecondField; 24 | mContext = mSourceView.getContext(); 25 | mErrorMessage = CONFIRM_ERROR_MESSAGE; 26 | } 27 | 28 | public ConfirmValidate(TextView field1, TextView field2, int errorMessageResource ) { 29 | mFirstField = field1; 30 | mSecondField = field2; 31 | mSourceView = mSecondField; 32 | mContext = mSourceView.getContext(); 33 | mErrorMessage = errorMessageResource; 34 | } 35 | 36 | @Override 37 | public boolean isValid() { 38 | final String firstFieldTxt = mFirstField.getText().toString(); 39 | final String secondFieldTxt = mSecondField.getText().toString(); 40 | if (isNotEmpty(firstFieldTxt) && firstFieldTxt.equals(secondFieldTxt)) { 41 | mSourceView.setError(null); 42 | return true; 43 | } else { 44 | mSourceView.setError(mContext.getString(mErrorMessage)); 45 | return false; 46 | } 47 | } 48 | 49 | public TextView getSource() { 50 | return mSourceView; 51 | } 52 | 53 | private boolean isNotEmpty(String text) { 54 | return !TextUtils.isEmpty(text); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validate/OrTwoRequiredValidate.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validate; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.throrinstudio.android.common.libs.validator.AbstractValidate; 7 | import com.throrinstudio.android.common.libs.validator.Validate; 8 | import com.throrinstudio.android.common.libs.validator.validator.NotEmptyValidator; 9 | 10 | /** 11 | * Validator class to validate if the fields are empty fields of 2 or not. 12 | * If one of them is null, no error. 13 | * If both are nulls: Error 14 | * 15 | * @author throrin19 16 | */ 17 | public class OrTwoRequiredValidate extends AbstractValidate { 18 | 19 | private TextView mFirstField; 20 | private TextView mSecondField; 21 | private Context mContext; 22 | 23 | public OrTwoRequiredValidate(TextView field1, TextView field2) { 24 | mFirstField = field1; 25 | mSecondField = field2; 26 | mContext = mFirstField.getContext(); 27 | } 28 | 29 | @Override 30 | public boolean isValid() { 31 | Validate firstFieldValidator = new Validate(mFirstField); 32 | NotEmptyValidator notEmptyValidator = new NotEmptyValidator(mContext); 33 | firstFieldValidator.addValidator(notEmptyValidator); 34 | 35 | Validate secondFieldValidator = new Validate(mSecondField); 36 | secondFieldValidator.addValidator(notEmptyValidator); 37 | 38 | if (firstFieldValidator.isValid() || secondFieldValidator.isValid()) { 39 | mFirstField.setError(null); 40 | return true; 41 | } else { 42 | mFirstField.setError(notEmptyValidator.getMessage()); 43 | return false; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/AlnumValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | import android.text.TextUtils; 6 | 7 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 8 | import com.throrinstudio.android.common.libs.validator.R; 9 | 10 | /** 11 | * Validator to check if a field contains only numbers and letters. 12 | * Avoids having special characters like accents. 13 | */ 14 | public class AlnumValidator extends AbstractValidator { 15 | 16 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_alnum; 17 | 18 | public AlnumValidator(Context c) { 19 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 20 | } 21 | 22 | public AlnumValidator(Context c, int errorMessageRes) { 23 | super(c, errorMessageRes); 24 | } 25 | 26 | public AlnumValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 27 | super(c, errorMessageRes, errorDrawable); 28 | } 29 | 30 | @Override 31 | public boolean isValid(String text) { 32 | return TextUtils.isDigitsOnly(text); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/EmailValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | import android.content.Context; 7 | import android.graphics.drawable.Drawable; 8 | import android.text.TextUtils; 9 | 10 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 11 | import com.throrinstudio.android.common.libs.validator.R; 12 | 13 | public class EmailValidator extends AbstractValidator { 14 | 15 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_email; 16 | private String mDomainName = ""; 17 | 18 | public EmailValidator(Context c) { 19 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 20 | } 21 | 22 | public EmailValidator(Context c, int errorMessageRes) { 23 | super(c, errorMessageRes); 24 | } 25 | 26 | public EmailValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 27 | super(c, errorMessageRes, errorDrawable); 28 | } 29 | 30 | /** 31 | * Lets say that the email address must be valid for such domain. 32 | * This function only accepts strings of Regexp 33 | * 34 | * @param domainName Regexp Domain Name 35 | *

36 | * example : gmail.com 37 | */ 38 | public void setDomainName(String domainName) { 39 | mDomainName = domainName; 40 | } 41 | 42 | @Override 43 | public boolean isValid(String email) { 44 | if (isNotEmpty(email)) { 45 | if (isNotEmpty(mDomainName)) { 46 | Pattern pattern = Pattern.compile(".+@" + mDomainName); 47 | Matcher matcher = pattern.matcher(email); 48 | return matcher.matches(); 49 | } else { 50 | Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+"); 51 | Matcher matcher = pattern.matcher(email); 52 | return matcher.matches(); 53 | } 54 | } 55 | return false; 56 | } 57 | 58 | private boolean isNotEmpty(String text) { 59 | return !TextUtils.isEmpty(text); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/HexValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | import android.content.Context; 6 | import android.graphics.drawable.Drawable; 7 | 8 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 9 | import com.throrinstudio.android.common.libs.validator.R; 10 | 11 | public class HexValidator extends AbstractValidator { 12 | 13 | private static final Pattern HEX_PATTERN = Pattern.compile("^(#|)[0-9A-Fa-f]+$"); 14 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_hex; 15 | 16 | public HexValidator(Context c) { 17 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 18 | } 19 | 20 | public HexValidator(Context c, int errorMessageRes) { 21 | super(c, errorMessageRes); 22 | } 23 | 24 | public HexValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 25 | super(c, errorMessageRes, errorDrawable); 26 | } 27 | 28 | @Override 29 | public boolean isValid(String text) { 30 | return HEX_PATTERN.matcher(text).matches(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/IPAddressValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | import android.content.Context; 7 | import android.graphics.drawable.Drawable; 8 | 9 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 10 | import com.throrinstudio.android.common.libs.validator.R; 11 | 12 | /** 13 | * Validates whether ip digits is between 0-255 digits, has only numbers and is the correct format 14 | * 15 | * @author Dixon D'Cunha (Exikle) 16 | */ 17 | public class IPAddressValidator extends AbstractValidator { 18 | 19 | /** 20 | * IP Addresses format 21 | */ 22 | private static final String IPADDRESS_STRING_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 23 | + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 24 | + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." 25 | + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; 26 | 27 | /** 28 | * Compiled pattern for the format 29 | */ 30 | private final Pattern IPADDRESS_PATTERN = Pattern 31 | .compile(IPADDRESS_STRING_PATTERN); 32 | 33 | /** 34 | * Default message if none specified 35 | */ 36 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_ip; 37 | 38 | public IPAddressValidator(Context c) { 39 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 40 | } 41 | 42 | public IPAddressValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 43 | super(c, errorMessageRes, errorDrawable); 44 | } 45 | 46 | @Override 47 | public boolean isValid(String ip) { 48 | return IPADDRESS_PATTERN.matcher(ip).matches(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/MockValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | 6 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 7 | 8 | /** 9 | * Just a mock validator to use as a coding place holder. Always returns true. 10 | * 11 | * @author Dixon D'Cunha (Exikle) 12 | */ 13 | public class MockValidator extends AbstractValidator { 14 | 15 | /** 16 | * Sets the error as "NO ERROR" 17 | */ 18 | private static final String DEFAULT_ERROR_MESSAGE_RESOURCE = "NO ERROR"; 19 | 20 | public MockValidator(Context c) { 21 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 22 | } 23 | 24 | public MockValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 25 | super(c, errorMessageRes, errorDrawable); 26 | } 27 | 28 | @Override 29 | public boolean isValid(String value) { 30 | return true; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/NotEmptyValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | import android.text.TextUtils; 6 | 7 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 8 | import com.throrinstudio.android.common.libs.validator.R; 9 | 10 | public class NotEmptyValidator extends AbstractValidator { 11 | 12 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_empty; 13 | 14 | public NotEmptyValidator(Context c) { 15 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 16 | } 17 | 18 | public NotEmptyValidator(Context c, int errorMessage) { 19 | super(c, errorMessage); 20 | } 21 | 22 | public NotEmptyValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 23 | super(c, errorMessageRes, errorDrawable); 24 | } 25 | 26 | @Override 27 | public boolean isValid(String text) { 28 | return !TextUtils.isEmpty(text); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/NumericValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | 6 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 7 | import com.throrinstudio.android.common.libs.validator.R; 8 | 9 | /** 10 | * Validates if the inputed values are all numeric. 11 | * 12 | * @author Dixon D'Cunha (Exikle) 13 | */ 14 | public class NumericValidator extends AbstractValidator { 15 | 16 | /** 17 | * Default message if none specified 18 | */ 19 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_ip; 20 | 21 | public NumericValidator(Context c) { 22 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 23 | } 24 | 25 | public NumericValidator(Context c, int errorMessage) { 26 | super(c, errorMessage); 27 | } 28 | 29 | public NumericValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 30 | super(c, errorMessageRes, errorDrawable); 31 | } 32 | 33 | @Override 34 | public boolean isValid(String str) { 35 | return containsOnlyNumbers(str); 36 | } 37 | 38 | /** 39 | * Checks if string has anything that isn't numbers. 40 | * 41 | * @param str 42 | * @return false only if null or not numeric 43 | */ 44 | private boolean containsOnlyNumbers(String str) { 45 | if (str == null || str.length() == 0) 46 | return false; 47 | for (int i = 0; i < str.length(); i++) { 48 | if (!Character.isDigit(str.charAt(i))) 49 | return false; 50 | } 51 | return true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/PhoneValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | import android.content.Context; 6 | import android.graphics.drawable.Drawable; 7 | import android.util.Patterns; 8 | 9 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 10 | import com.throrinstudio.android.common.libs.validator.ValidatorException; 11 | import com.throrinstudio.android.common.libs.validator.R; 12 | 13 | /** 14 | * Validator to check if Phone number is correct. 15 | * Created by throrin19 on 13/06/13. 16 | */ 17 | public class PhoneValidator extends AbstractValidator { 18 | 19 | private static final Pattern PHONE_PATTERN = Patterns.PHONE; 20 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_phone; 21 | 22 | public PhoneValidator(Context c) { 23 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 24 | } 25 | 26 | public PhoneValidator(Context c, int errorMessageRes) { 27 | super(c, errorMessageRes); 28 | } 29 | 30 | public PhoneValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 31 | super(c, errorMessageRes, errorDrawable); 32 | } 33 | 34 | @Override 35 | public boolean isValid(String phone) throws ValidatorException { 36 | return PHONE_PATTERN.matcher(phone).matches(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/RangeValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | 6 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 7 | import com.throrinstudio.android.common.libs.validator.R; 8 | 9 | /** 10 | * Validates whether the given value is between a range of values 11 | * 12 | * @author Dixon D'Cunha (Exikle) 13 | */ 14 | public class RangeValidator extends AbstractValidator { 15 | 16 | /** 17 | * The start of the range 18 | */ 19 | final double START_RANGE; 20 | 21 | /** 22 | * The end of the range 23 | */ 24 | final double END_RANGE; 25 | 26 | /** 27 | * The error Id from the string resource 28 | */ 29 | private static int mErrorMessage; // Your custom error message 30 | 31 | /** 32 | * Default error message if none specified 33 | */ 34 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_range; 35 | 36 | public RangeValidator(Context c, double start, double end) { 37 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 38 | START_RANGE = start; 39 | END_RANGE = end; 40 | } 41 | 42 | public RangeValidator(Context c, int errorMessageRes, Drawable errorDrawable, double start, double end) { 43 | super(c, errorMessageRes, errorDrawable); 44 | START_RANGE = start; 45 | END_RANGE = end; 46 | } 47 | 48 | /** 49 | * @param context 50 | * @param start 51 | * of the range 52 | * @param end 53 | * of the range 54 | * @param errorMessageRes 55 | */ 56 | public RangeValidator(Context c, double start, double end, 57 | int errorMessageRes) { 58 | super(c, errorMessageRes); 59 | mErrorMessage = errorMessageRes; 60 | START_RANGE = start; 61 | END_RANGE = end; 62 | } 63 | 64 | @Override 65 | public String getMessage() { 66 | return getContext().getString(mErrorMessage); 67 | } 68 | 69 | /** 70 | * Checks is value is between given range 71 | * @return true if between range; false if outside of range 72 | */ 73 | @Override 74 | public boolean isValid(String value) { 75 | if (value != null && value.length() > 0) { 76 | double inputedSize = Double.parseDouble(value); 77 | return inputedSize >= START_RANGE 78 | && inputedSize <= END_RANGE; 79 | } else 80 | return false; 81 | } 82 | } -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/RegExpValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | import android.content.Context; 6 | import android.graphics.drawable.Drawable; 7 | 8 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 9 | import com.throrinstudio.android.common.libs.validator.ValidatorException; 10 | import com.throrinstudio.android.common.libs.validator.R; 11 | 12 | /** 13 | * This validator test value with custom Regex Pattern. 14 | */ 15 | public class RegExpValidator extends AbstractValidator { 16 | 17 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_regexp; 18 | private Pattern mPattern; 19 | 20 | public RegExpValidator(Context c) { 21 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 22 | } 23 | 24 | public RegExpValidator(Context c, int errorMessageRes) { 25 | super(c, errorMessageRes); 26 | } 27 | 28 | public RegExpValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 29 | super(c, errorMessageRes, errorDrawable); 30 | } 31 | 32 | public void setPattern(String pattern) { 33 | mPattern = Pattern.compile(pattern); 34 | } 35 | 36 | public void setPattern(Pattern pattern) { 37 | mPattern = pattern; 38 | } 39 | 40 | @Override 41 | public boolean isValid(String text) throws ValidatorException { 42 | if (mPattern != null) { 43 | return mPattern.matcher(text).matches(); 44 | } 45 | throw new ValidatorException("You can set Regexp Pattern first"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /library/src/com/throrinstudio/android/common/libs/validator/validator/UrlValidator.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | import android.content.Context; 6 | import android.graphics.drawable.Drawable; 7 | import android.util.Patterns; 8 | 9 | import com.throrinstudio.android.common.libs.validator.AbstractValidator; 10 | import com.throrinstudio.android.common.libs.validator.R; 11 | 12 | public class UrlValidator extends AbstractValidator { 13 | 14 | private static final Pattern WEB_URL_PATTERN = Patterns.WEB_URL; 15 | private static final int DEFAULT_ERROR_MESSAGE_RESOURCE = R.string.validator_url; 16 | 17 | public UrlValidator(Context c) { 18 | super(c, DEFAULT_ERROR_MESSAGE_RESOURCE); 19 | } 20 | 21 | public UrlValidator(Context c, int errorMessageRes) { 22 | super(c, errorMessageRes); 23 | } 24 | 25 | public UrlValidator(Context c, int errorMessageRes, Drawable errorDrawable) { 26 | super(c, errorMessageRes, errorDrawable); 27 | } 28 | 29 | @Override 30 | public boolean isValid(String url) { 31 | return WEB_URL_PATTERN.matcher(url).matches(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/FormTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.throrinstudio.android.common.libs.validator.Validate; 7 | import com.throrinstudio.android.common.libs.validator.Form; 8 | import com.throrinstudio.android.common.libs.validator.validator.AlnumValidator; 9 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 10 | import com.throrinstudio.android.common.libs.validator.validator.HexValidator; 11 | import com.throrinstudio.android.common.libs.validator.validator.NotEmptyValidator; 12 | import com.throrinstudio.android.common.libs.validator.validator.PhoneValidator; 13 | import com.throrinstudio.android.common.libs.validator.validator.UrlValidator; 14 | 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.robolectric.Robolectric; 18 | import org.robolectric.RobolectricTestRunner; 19 | 20 | import static org.junit.Assert.*; 21 | 22 | /** 23 | * Created by piobab on 03.04.2014. 24 | */ 25 | @RunWith(RobolectricGradleTestRunner.class) 26 | public class FormTest { 27 | 28 | @Test 29 | public void validateFormWithoutAnyValidates() throws Exception { 30 | Form form = new Form(); 31 | assertTrue(form.validate()); 32 | } 33 | 34 | @Test 35 | public void validateFormWithNotEmptyAndEmailAndPhoneValidators() throws Exception { 36 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 37 | 38 | TextView notEmptyField = new TextView(context); 39 | notEmptyField.setText("validate"); 40 | Validate notEmptyValidate = new Validate(notEmptyField); 41 | notEmptyValidate.addValidator(new NotEmptyValidator(context)); 42 | 43 | TextView emailField = new TextView(context); 44 | emailField.setText("email@gmail.com"); 45 | Validate emailValidate = new Validate(emailField); 46 | emailValidate.addValidator(new EmailValidator(context)); 47 | 48 | TextView phoneField = new TextView(context); 49 | phoneField.setText("+46123456789"); 50 | Validate phoneValidate = new Validate(phoneField); 51 | phoneValidate.addValidator(new PhoneValidator(context)); 52 | 53 | Form form = new Form(); 54 | form.addValidates(notEmptyValidate); 55 | form.addValidates(emailValidate); 56 | form.addValidates(phoneValidate); 57 | assertTrue(form.validate()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/RobolectricGradleTestRunner.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import org.junit.runners.model.InitializationError; 4 | import org.robolectric.AndroidManifest; 5 | import org.robolectric.RobolectricTestRunner; 6 | import org.robolectric.annotation.Config; 7 | import org.robolectric.res.Fs; 8 | import org.robolectric.res.FsFile; 9 | 10 | /** 11 | * Created by pbabel on 03.04.2014. 12 | */ 13 | public class RobolectricGradleTestRunner extends RobolectricTestRunner { 14 | public RobolectricGradleTestRunner(Class testClass) throws InitializationError { 15 | super(testClass); 16 | } 17 | 18 | @Override protected AndroidManifest getAppManifest(Config config) { 19 | String manifestProperty = System.getProperty("android.manifest"); 20 | if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) { 21 | String resProperty = System.getProperty("android.resources"); 22 | String assetsProperty = System.getProperty("android.assets"); 23 | return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty), 24 | Fs.fileFromPath(assetsProperty)); 25 | } 26 | return super.getAppManifest(config); 27 | } 28 | } -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/ValidateText.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.throrinstudio.android.common.libs.validator.Validate; 7 | import com.throrinstudio.android.common.libs.validator.validator.AlnumValidator; 8 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 9 | import com.throrinstudio.android.common.libs.validator.validator.HexValidator; 10 | import com.throrinstudio.android.common.libs.validator.validator.NotEmptyValidator; 11 | import com.throrinstudio.android.common.libs.validator.validator.PhoneValidator; 12 | import com.throrinstudio.android.common.libs.validator.validator.UrlValidator; 13 | 14 | import org.junit.Test; 15 | import org.junit.Before; 16 | import org.junit.runner.RunWith; 17 | import org.robolectric.Robolectric; 18 | import org.robolectric.RobolectricTestRunner; 19 | 20 | import static org.junit.Assert.*; 21 | 22 | /** 23 | * Created by piobab on 03.04.2014. 24 | */ 25 | @RunWith(RobolectricGradleTestRunner.class) 26 | public class ValidateText { 27 | 28 | private Context mContext; 29 | private TextView mField; 30 | 31 | @Before 32 | public void setup() { 33 | mContext = Robolectric.getShadowApplication().getApplicationContext(); 34 | mField = new TextView(mContext); 35 | } 36 | 37 | @Test 38 | public void validateFieldWithoutAnyValidators() throws Exception { 39 | Validate validate = new Validate(mField); 40 | assertTrue(validate.isValid()); 41 | } 42 | 43 | @Test 44 | public void validateFieldWithNotEmptyValidator() throws Exception { 45 | mField.setText("validate"); 46 | Validate validate = new Validate(mField); 47 | validate.addValidator(new NotEmptyValidator(mContext)); 48 | assertTrue(validate.isValid()); 49 | mField.setText(""); 50 | assertFalse(validate.isValid()); 51 | } 52 | 53 | @Test 54 | public void validateFieldWithNotEmptyAndAlnumValidators() throws Exception { 55 | mField.setText("1234567890"); 56 | Validate validate = new Validate(mField); 57 | validate.addValidator(new NotEmptyValidator(mContext)); 58 | validate.addValidator(new AlnumValidator(mContext)); 59 | assertTrue(validate.isValid()); 60 | mField.setText(""); 61 | assertFalse(validate.isValid()); 62 | mField.setText("abcdef"); 63 | assertFalse(validate.isValid()); 64 | } 65 | 66 | @Test 67 | public void validateFieldWithNotEmptyAndEmailValidators() throws Exception { 68 | mField.setText("email@yahoo.com"); 69 | Validate validate = new Validate(mField); 70 | validate.addValidator(new NotEmptyValidator(mContext)); 71 | validate.addValidator(new EmailValidator(mContext)); 72 | assertTrue(validate.isValid()); 73 | mField.setText(""); 74 | assertFalse(validate.isValid()); 75 | mField.setText("@gmail.com"); 76 | assertFalse(validate.isValid()); 77 | } 78 | 79 | @Test 80 | public void validateFieldWithNotEmptyAndPhoneValidators() throws Exception { 81 | mField.setText("+48600123456"); 82 | Validate validate = new Validate(mField); 83 | validate.addValidator(new NotEmptyValidator(mContext)); 84 | validate.addValidator(new PhoneValidator(mContext)); 85 | assertTrue(validate.isValid()); 86 | mField.setText(""); 87 | assertFalse(validate.isValid()); 88 | mField.setText("abcde_fgh"); 89 | assertFalse(validate.isValid()); 90 | } 91 | 92 | @Test 93 | public void validateFieldWithNotEmptyAndHexValidators() throws Exception { 94 | mField.setText("1234567890abcdef"); 95 | Validate validate = new Validate(mField); 96 | validate.addValidator(new NotEmptyValidator(mContext)); 97 | validate.addValidator(new HexValidator(mContext)); 98 | assertTrue(validate.isValid()); 99 | mField.setText(""); 100 | assertFalse(validate.isValid()); 101 | mField.setText("abcdefg"); 102 | assertFalse(validate.isValid()); 103 | } 104 | 105 | @Test 106 | public void validateFieldWithNotEmptyAndUrlValidators() throws Exception { 107 | mField.setText("http://www.github.com/test/"); 108 | Validate validate = new Validate(mField); 109 | validate.addValidator(new NotEmptyValidator(mContext)); 110 | validate.addValidator(new UrlValidator(mContext)); 111 | assertTrue(validate.isValid()); 112 | mField.setText(""); 113 | assertFalse(validate.isValid()); 114 | mField.setText("test"); 115 | assertFalse(validate.isValid()); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validate/ConfirmValidateTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validate; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.throrinstudio.android.common.libs.validator.validate.ConfirmValidate; 7 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 8 | 9 | import org.junit.Test; 10 | import org.junit.Before; 11 | import org.junit.runner.RunWith; 12 | import org.robolectric.Robolectric; 13 | import org.robolectric.RobolectricTestRunner; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | /** 18 | * Created by piobab on 03.04.2014. 19 | */ 20 | @RunWith(RobolectricGradleTestRunner.class) 21 | public class ConfirmValidateTest { 22 | 23 | private Context mContext; 24 | private TextView mFirstField; 25 | private TextView mSecondField; 26 | 27 | @Before 28 | public void setup() { 29 | mContext = Robolectric.getShadowApplication().getApplicationContext(); 30 | mFirstField = new TextView(mContext); 31 | mSecondField = new TextView(mContext); 32 | } 33 | 34 | @Test 35 | public void validateEmptyFields() throws Exception { 36 | mFirstField.setText(""); 37 | mSecondField.setText(""); 38 | 39 | ConfirmValidate confirmValidate = new ConfirmValidate(mFirstField, mSecondField); 40 | assertFalse(confirmValidate.isValid()); 41 | } 42 | 43 | @Test 44 | public void validateFirstFieldEmpty() throws Exception { 45 | mFirstField.setText(""); 46 | mSecondField.setText("abcdef"); 47 | 48 | ConfirmValidate confirmValidate = new ConfirmValidate(mFirstField, mSecondField); 49 | assertFalse(confirmValidate.isValid()); 50 | } 51 | 52 | @Test 53 | public void validateSecondFieldEmpty() throws Exception { 54 | mFirstField.setText("abcdef"); 55 | mSecondField.setText(""); 56 | 57 | ConfirmValidate confirmValidate = new ConfirmValidate(mFirstField, mSecondField); 58 | assertFalse(confirmValidate.isValid()); 59 | } 60 | 61 | @Test 62 | public void validate() throws Exception { 63 | mFirstField.setText("abcdef"); 64 | mSecondField.setText("abcdef"); 65 | 66 | ConfirmValidate confirmValidate = new ConfirmValidate(mFirstField, mSecondField); 67 | assertTrue(confirmValidate.isValid()); 68 | 69 | // Change second text 70 | mSecondField.setText("fedcba"); 71 | assertFalse(confirmValidate.isValid()); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validate/OrTwoRequiredValidateTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validate; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 7 | import com.throrinstudio.android.common.libs.validator.validate.OrTwoRequiredValidate; 8 | 9 | import org.junit.Test; 10 | import org.junit.Before; 11 | import org.junit.runner.RunWith; 12 | import org.robolectric.Robolectric; 13 | import org.robolectric.RobolectricTestRunner; 14 | 15 | import static org.junit.Assert.*; 16 | 17 | /** 18 | * Created by piobab on 03.04.2014. 19 | */ 20 | @RunWith(RobolectricGradleTestRunner.class) 21 | public class OrTwoRequiredValidateTest { 22 | 23 | private Context mContext; 24 | private TextView mFirstField; 25 | private TextView mSecondField; 26 | 27 | @Before 28 | public void setup() { 29 | mContext = Robolectric.getShadowApplication().getApplicationContext(); 30 | mFirstField = new TextView(mContext); 31 | mSecondField = new TextView(mContext); 32 | } 33 | 34 | @Test 35 | public void validateEmptyFields() throws Exception { 36 | mFirstField.setText(""); 37 | mSecondField.setText(""); 38 | 39 | OrTwoRequiredValidate orTwoRequiredValidate = new OrTwoRequiredValidate(mFirstField, mSecondField); 40 | assertFalse(orTwoRequiredValidate.isValid()); 41 | } 42 | 43 | @Test 44 | public void validateFirstFieldEmpty() throws Exception { 45 | mFirstField.setText(""); 46 | mSecondField.setText("abcdef"); 47 | 48 | OrTwoRequiredValidate orTwoRequiredValidate = new OrTwoRequiredValidate(mFirstField, mSecondField); 49 | assertTrue(orTwoRequiredValidate.isValid()); 50 | } 51 | 52 | @Test 53 | public void validateSecondFieldEmpty() throws Exception { 54 | mFirstField.setText("abcdef"); 55 | mSecondField.setText(""); 56 | 57 | OrTwoRequiredValidate orTwoRequiredValidate = new OrTwoRequiredValidate(mFirstField, mSecondField); 58 | assertTrue(orTwoRequiredValidate.isValid()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/AlnumValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.validator.AlnumValidator; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.robolectric.Robolectric; 11 | import org.robolectric.RobolectricTestRunner; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * Created by piobab on 03.04.2014. 17 | */ 18 | @RunWith(RobolectricGradleTestRunner.class) 19 | public class AlnumValidatorTest { 20 | 21 | @Test 22 | public void validateWithAllNumbers() throws Exception { 23 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 24 | AlnumValidator alnumValidator = new AlnumValidator(context); 25 | assertTrue(alnumValidator.isValid("1234567890")); 26 | assertFalse(alnumValidator.isValid("abcdef")); 27 | assertTrue(alnumValidator.isValid("")); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/EmailValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.robolectric.Robolectric; 11 | import org.robolectric.RobolectricTestRunner; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * Created by piobab on 03.04.2014. 17 | */ 18 | @RunWith(RobolectricGradleTestRunner.class) 19 | public class EmailValidatorTest { 20 | 21 | @Test 22 | public void validateEmailWithoutDomainApplied() throws Exception { 23 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 24 | EmailValidator emailValidator = new EmailValidator(context); 25 | assertTrue(emailValidator.isValid("email@yahoo.com")); 26 | assertFalse(emailValidator.isValid("abcdef")); 27 | assertFalse(emailValidator.isValid("")); 28 | assertFalse(emailValidator.isValid("email@")); 29 | assertFalse(emailValidator.isValid("@gmail.com")); 30 | } 31 | 32 | @Test 33 | public void validateEmailWithDomainApplied() throws Exception { 34 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 35 | EmailValidator emailValidator = new EmailValidator(context); 36 | emailValidator.setDomainName("gmail.com"); 37 | assertTrue(emailValidator.isValid("email@gmail.com")); 38 | assertFalse(emailValidator.isValid("email@yahoo.com")); 39 | assertFalse(emailValidator.isValid("abcdef")); 40 | assertFalse(emailValidator.isValid("")); 41 | assertFalse(emailValidator.isValid("email@")); 42 | assertFalse(emailValidator.isValid("@gmail.com")); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/HexValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.robolectric.Robolectric; 11 | import org.robolectric.RobolectricTestRunner; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * Created by piobab on 03.04.2014. 17 | */ 18 | @RunWith(RobolectricGradleTestRunner.class) 19 | public class HexValidatorTest { 20 | 21 | @Test 22 | public void validateHexNumber() throws Exception { 23 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 24 | HexValidator hexValidator = new HexValidator(context); 25 | assertTrue(hexValidator.isValid("1234567890abcdef")); 26 | assertTrue(hexValidator.isValid("#12FF")); 27 | assertFalse(hexValidator.isValid("abcdefg")); 28 | assertFalse(hexValidator.isValid("")); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/NotEmptyValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.robolectric.Robolectric; 11 | import org.robolectric.RobolectricTestRunner; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * Created by piobab on 03.04.2014. 17 | */ 18 | @RunWith(RobolectricGradleTestRunner.class) 19 | public class NotEmptyValidatorTest { 20 | 21 | @Test 22 | public void isNotEmpty() throws Exception { 23 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 24 | NotEmptyValidator notEmptyValidator = new NotEmptyValidator(context); 25 | assertTrue(notEmptyValidator.isValid("abcdef")); 26 | assertTrue(notEmptyValidator.isValid(" ")); 27 | assertFalse(notEmptyValidator.isValid("")); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/PhoneValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.robolectric.Robolectric; 11 | import org.robolectric.RobolectricTestRunner; 12 | 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * Created by piobab on 03.04.2014. 17 | */ 18 | @RunWith(RobolectricGradleTestRunner.class) 19 | public class PhoneValidatorTest { 20 | 21 | @Test 22 | public void validatePhoneNumber() throws Exception { 23 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 24 | PhoneValidator phoneValidator = new PhoneValidator(context); 25 | assertTrue(phoneValidator.isValid("048123456789")); 26 | assertTrue(phoneValidator.isValid("+48123456789")); 27 | assertTrue(phoneValidator.isValid("600456789")); 28 | assertTrue(phoneValidator.isValid("600456")); 29 | assertFalse(phoneValidator.isValid("")); 30 | assertFalse(phoneValidator.isValid("abcdef10")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/RegExpValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.ValidatorException; 7 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.robolectric.Robolectric; 12 | import org.robolectric.RobolectricTestRunner; 13 | 14 | import static org.junit.Assert.*; 15 | 16 | /** 17 | * Created by piobab on 03.04.2014. 18 | */ 19 | @RunWith(RobolectricGradleTestRunner.class) 20 | public class RegExpValidatorTest { 21 | 22 | @Test 23 | public void validateWithoutRegularExpressionSpecified() throws Exception { 24 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 25 | RegExpValidator regExpValidator = new RegExpValidator(context); 26 | boolean valid = true; 27 | try { 28 | // Should throw exception 29 | regExpValidator.isValid("1234567890abcdef"); 30 | } catch (ValidatorException ve) { 31 | valid = false; 32 | } 33 | assertFalse(valid); 34 | } 35 | 36 | @Test 37 | public void validateIpAddress() throws Exception { 38 | String ipAddressPattern = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$"; 39 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 40 | RegExpValidator regExpValidator = new RegExpValidator(context); 41 | regExpValidator.setPattern(ipAddressPattern); 42 | assertTrue(regExpValidator.isValid("127.0.0.1")); 43 | assertTrue(regExpValidator.isValid("255.0.0.0")); 44 | assertTrue(regExpValidator.isValid("255.255.255.255")); 45 | assertTrue(regExpValidator.isValid("0.0.0.0")); 46 | assertFalse(regExpValidator.isValid("127.0")); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /library/test/com/throrinstudio/android/common/libs/validator/validator/UrlValidatorTest.java: -------------------------------------------------------------------------------- 1 | package com.throrinstudio.android.common.libs.validator.validator; 2 | 3 | import android.content.Context; 4 | 5 | import com.throrinstudio.android.common.libs.validator.RobolectricGradleTestRunner; 6 | import com.throrinstudio.android.common.libs.validator.ValidatorException; 7 | import com.throrinstudio.android.common.libs.validator.validator.EmailValidator; 8 | 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.robolectric.Robolectric; 12 | import org.robolectric.RobolectricTestRunner; 13 | 14 | import static org.junit.Assert.*; 15 | 16 | /** 17 | * Created by piobab on 03.04.2014. 18 | */ 19 | @RunWith(RobolectricGradleTestRunner.class) 20 | public class UrlValidatorTest { 21 | 22 | @Test 23 | public void validateUrl() throws Exception { 24 | Context context = Robolectric.getShadowApplication().getApplicationContext(); 25 | UrlValidator urlValidator = new UrlValidator(context); 26 | assertTrue(urlValidator.isValid("www.google.com")); 27 | assertTrue(urlValidator.isValid("http://www.wp.pl/")); 28 | assertTrue(urlValidator.isValid("http://github.com")); 29 | assertFalse(urlValidator.isValid("htp://twitter.com")); 30 | assertFalse(urlValidator.isValid("http://twitter")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /others/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/throrin19/Android-Validator/ab6c0ef6d94c7a6c5ce48002676819e9b5fe9ea0/others/ic_launcher-web.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library' 2 | --------------------------------------------------------------------------------