1
1
package com.arbazmateen.validator
2
2
3
+ import android.util.Patterns
3
4
import android.widget.EditText
4
5
5
6
/* *************************************************************************
6
- ** Extension functions
7
- **************************************************************************/
7
+ ** Extension functions
8
+ **************************************************************************/
8
9
fun EditText.validator () = InputValidator (this .text.toString().trim())
9
10
10
11
/* *************************************************************************
11
- ** Edit text validator class
12
- **************************************************************************/
12
+ ** Edit text validator class
13
+ **************************************************************************/
13
14
14
15
class InputValidator (val text : String ) {
15
16
@@ -21,36 +22,36 @@ class InputValidator(val text: String) {
21
22
22
23
private var errorMessage = " "
23
24
24
- private var successListener : (() -> Unit )? = null
25
- private var errorListener : ((error: String ) -> Unit )? = null
25
+ private var validListener : (() -> Unit )? = null
26
+ private var invalidListener : ((error: String ) -> Unit )? = null
26
27
27
28
fun validate (): Boolean {
28
- if (isValid) {
29
- successListener ?.invoke()
29
+ if (isValid) {
30
+ validListener ?.invoke()
30
31
} else {
31
- errorListener ?.invoke(errorMessage)
32
+ invalidListener ?.invoke(errorMessage)
32
33
}
33
34
34
35
return isValid
35
36
}
36
37
37
38
fun required (): InputValidator {
38
- if (text.isEmpty()) {
39
+ if (text.isEmpty()) {
39
40
setErrorMessage(" Required field." )
40
41
}
41
42
return this
42
43
}
43
44
44
45
fun required (regex : String , error : String = "Invalid input"): InputValidator {
45
- if (! text.matches(Regex (regex))) {
46
+ if (! text.matches(Regex (regex))) {
46
47
setErrorMessage(error)
47
48
}
48
49
return this
49
50
}
50
51
51
52
fun allow (regex : String , error : String = "Invalid input"): InputValidator {
52
- if (isOptional()) return this
53
- if (! text.matches(Regex (regex))) {
53
+ if (isOptional()) return this
54
+ if (! text.matches(Regex (regex))) {
54
55
setErrorMessage(error)
55
56
}
56
57
return this
@@ -62,30 +63,179 @@ class InputValidator(val text: String) {
62
63
return this
63
64
}
64
65
66
+ fun optional (regex : String ): InputValidator {
67
+ isOptional = true
68
+ minimumLength = 0
69
+ allow(regex)
70
+ return this
71
+ }
72
+
73
+ fun validateEmail (builtInCheck : Boolean = false): InputValidator {
74
+ if (isOptional()) return this
75
+ if (builtInCheck && ! (text.matches(Regex (REGEX_EMAIL )) || Patterns .EMAIL_ADDRESS .matcher(text).matches())) {
76
+ setErrorMessage(" Invalid email address." )
77
+ } else if (! text.matches(Regex (REGEX_EMAIL ))) {
78
+ setErrorMessage(" Invalid email address." )
79
+ }
80
+ return this
81
+ }
82
+
83
+ fun validatePassword (allowSpace : Boolean = true): InputValidator {
84
+ if (isOptional()) return this
85
+ val regex = if (allowSpace) {
86
+ REGEX_PASSWORD_CUSTOM_LENGTH = " $minimumLength ,$maximumLength "
87
+ REGEX_PASSWORD_CUSTOM_LENGTH
88
+ } else {
89
+ REGEX_PASSWORD_WO_SPACE_CUSTOM_LENGTH = " $minimumLength ,$maximumLength "
90
+ REGEX_PASSWORD_WO_SPACE_CUSTOM_LENGTH
91
+ }
92
+ if (! text.matches(Regex (regex))) {
93
+ var e = " Must contains:\n Upper Letters,\n Lower Letters,\n Numbers," +
94
+ " \n Special Characters,\n Min Length: $minimumLength ,\n Max Length: $maximumLength "
95
+ if (! allowSpace) e + = " ,\n Space not allowed"
96
+ setErrorMessage(e)
97
+ }
98
+ return this
99
+ }
100
+
101
+ fun validateName (allowSpace : Boolean = false,
102
+ allowHyphen : Boolean = false,
103
+ allowDot : Boolean = false,
104
+ allowUnderscore : Boolean = false): InputValidator {
105
+ if (isOptional()) return this
106
+ var error = " Invalid name:\n Only characters allowed"
107
+ val regex = when {
108
+ allowSpace && allowHyphen && allowDot && allowUnderscore -> {
109
+ error = " Invalid name:\n Only characters, Space, (-), (.), (_) allowed"
110
+ REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT__
111
+ }
112
+ allowSpace && allowHyphen && allowDot -> {
113
+ error = " Invalid name:\n Only characters, Space, (-), (.) allowed"
114
+ REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT
115
+ }
116
+ allowSpace && allowHyphen -> {
117
+ error = " Invalid name:\n Only characters, Space, (-) allowed"
118
+ REGEX_CHARACTERS_W_SPACE_HYPHEN
119
+ }
120
+ allowSpace -> {
121
+ error = " Invalid name:\n Only characters, Space allowed"
122
+ REGEX_CHARACTERS_W_SPACE
123
+ }
124
+ else -> {
125
+ REGEX_CHARACTERS
126
+ }
127
+ }
128
+ if (! text.matches(Regex (regex))) {
129
+ setErrorMessage(error)
130
+ }
131
+ return this
132
+ }
133
+
134
+ fun validateText (allowNumbers : Boolean = false,
135
+ allowSpace : Boolean = false,
136
+ allowHyphen : Boolean = false,
137
+ allowDot : Boolean = false,
138
+ allowUnderscore : Boolean = false): InputValidator {
139
+ if (isOptional()) return this
140
+ var error = " Invalid name:\n Only characters allowed"
141
+ val regex = when {
142
+ allowNumbers -> {
143
+ when {
144
+ allowSpace && allowHyphen && allowDot && allowUnderscore -> {
145
+ error = " Invalid name:\n Only numbers, characters,\n Space, (-), (.) & (_) allowed"
146
+ REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN_DOT__
147
+ }
148
+ allowSpace && allowHyphen && allowDot -> {
149
+ error = " Invalid name:\n Only numbers, characters,\n Space, (-) & (.) allowed"
150
+ REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN_DOT
151
+ }
152
+ allowSpace && allowHyphen -> {
153
+ error = " Invalid name:\n Only numbers, characters,\n Space & (-) allowed"
154
+ REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN
155
+ }
156
+ allowSpace -> {
157
+ error = " Invalid name:\n Only numbers, characters & Space allowed"
158
+ REGEX_ALPHA_NUMERIC_W_SPACE
159
+ }
160
+ else -> {
161
+ error = " Invalid name:\n Only numbers & characters allowed"
162
+ REGEX_ALPHA_NUMERIC
163
+ }
164
+ }
165
+ }
166
+ allowSpace && allowHyphen && allowDot && allowUnderscore -> {
167
+ error = " Invalid name:\n Only characters, Space,\n (-), (.) & (_) allowed"
168
+ REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT__
169
+ }
170
+ allowSpace && allowHyphen && allowDot -> {
171
+ error = " Invalid name:\n Only characters, Space,\n (-) & (.) allowed"
172
+ REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT
173
+ }
174
+ allowSpace && allowHyphen -> {
175
+ error = " Invalid name:\n Only characters, Space & (-) allowed"
176
+ REGEX_CHARACTERS_W_SPACE_HYPHEN
177
+ }
178
+ allowSpace -> {
179
+ error = " Invalid name:\n Only characters & Space allowed"
180
+ REGEX_CHARACTERS_W_SPACE
181
+ }
182
+ else -> {
183
+ REGEX_CHARACTERS
184
+ }
185
+ }
186
+ if (! text.matches(Regex (regex))) {
187
+ setErrorMessage(error)
188
+ }
189
+ return this
190
+ }
191
+
192
+ fun validateNumbers (allowNegative : Boolean = false): InputValidator {
193
+ if (isOptional()) return this
194
+ val regex = if (allowNegative) REGEX_NEGATIVE_NUMBERS else REGEX_NUMBERS
195
+ if (! text.matches(Regex (regex))) {
196
+ setErrorMessage(" Only numbers allowed" )
197
+ }
198
+ return this
199
+ }
200
+
201
+ fun validateFloatingPointNumbers (allowNegative : Boolean = false): InputValidator {
202
+ if (isOptional()) return this
203
+ val regex = if (allowNegative) REGEX_TWO_FLOATING_POINT else REGEX_POSITIVE_TWO_FLOATING_POINT
204
+ if (! text.matches(Regex (regex))) {
205
+ setErrorMessage(" Only floating point numbers with 2 decimal allowed" )
206
+ }
207
+ return this
208
+ }
209
+
65
210
fun minimumLength (length : Int ): InputValidator {
66
211
minimumLength = length
67
- if (isOptional()) return this
68
- if (text.length < minimumLength) {
212
+ if (isOptional()) return this
213
+ if (text.length < minimumLength) {
69
214
setErrorMessage(" Minimum length required: $minimumLength " )
70
215
}
71
216
return this
72
217
}
73
218
74
219
fun maximumLength (length : Int ): InputValidator {
75
220
maximumLength = length
76
- if (text.length > maximumLength) {
221
+ if (text.length > maximumLength) {
77
222
setErrorMessage(" Maximum length allowed: $maximumLength " )
78
223
}
79
224
return this
80
225
}
81
226
82
- fun setSuccessListener (successListener : () -> Unit ): InputValidator {
83
- this .successListener = successListener
227
+ fun setValidListener (successListener : () -> Unit ): InputValidator {
228
+ this .validListener = successListener
229
+ return this
230
+ }
231
+
232
+ fun setInvalidListener (errorListener : (error: String ) -> Unit ): InputValidator {
233
+ this .invalidListener = errorListener
84
234
return this
85
235
}
86
236
87
- fun setErrorListener ( errorListener : ( error: String ) -> Unit ): InputValidator {
88
- this .errorListener = errorListener
237
+ fun setCustomErrorMessage ( error : String ): InputValidator {
238
+ errorMessage = error
89
239
return this
90
240
}
91
241
0 commit comments