Skip to content

Commit 29d43cf

Browse files
committed
validator module ready
1 parent 89dc975 commit 29d43cf

File tree

8 files changed

+272
-25
lines changed

8 files changed

+272
-25
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dialogs/src/main/java/com/arbazmateen/dialogs/NotificationDialogs.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package com.arbazmateen.dialogs
33
import android.content.Context
44
import androidx.appcompat.app.AlertDialog
55

6-
const val INFO_ICON = 0
7-
const val WARNING_ICON = 0
8-
const val ERROR_ICON = 0
9-
const val DELETE_ICON = 0
6+
val INFO_ICON = R.drawable.ic_info
7+
val WARNING_ICON = R.drawable.ic_warning
8+
val ERROR_ICON = R.drawable.ic_error
9+
val DELETE_ICON = R.drawable.ic_delete
1010

1111
fun dialog(context: Context, title: Int, message: String, cancelAble: Boolean, icon: Int): AlertDialog.Builder {
1212
val dialogBuilder = AlertDialog.Builder(context)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportHeight="138"
5+
android:viewportWidth="138">
6+
<path
7+
android:fillColor="#C93636"
8+
android:pathData="M64,64m-64,0a64,64 0,1 1,128 0a64,64 0,1 1,-128 0" />
9+
<path
10+
android:fillColor="#FFFFFF"
11+
android:pathData="M100.3,90.4L73.9,64l26.3,-26.4c0.4,-0.4 0.4,-1 0,-1.4l-8.5,-8.5c-0.4,-0.4 -1,-0.4 -1.4,0L64,54.1L37.7,27.8c-0.4,-0.4 -1,-0.4 -1.4,0l-8.5,8.5c-0.4,0.4 -0.4,1 0,1.4L54,64L27.7,90.3c-0.4,0.4 -0.4,1 0,1.4l8.5,8.5c0.4,0.4 1.1,0.4 1.4,0L64,73.9l26.3,26.3c0.4,0.4 1.1,0.4 1.5,0.1l8.5,-8.5C100.7,91.4 100.7,90.8 100.3,90.4z" />
12+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#c0392b"
8+
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
9+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#898989"
8+
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
9+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF8C00"
8+
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
9+
</vector>

validator/src/main/java/com/arbazmateen/validator/InputValidator.kt

+170-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.arbazmateen.validator
22

3+
import android.util.Patterns
34
import android.widget.EditText
45

56
/**************************************************************************
6-
** Extension functions
7-
**************************************************************************/
7+
** Extension functions
8+
**************************************************************************/
89
fun EditText.validator() = InputValidator(this.text.toString().trim())
910

1011
/**************************************************************************
11-
** Edit text validator class
12-
**************************************************************************/
12+
** Edit text validator class
13+
**************************************************************************/
1314

1415
class InputValidator(val text: String) {
1516

@@ -21,36 +22,36 @@ class InputValidator(val text: String) {
2122

2223
private var errorMessage = ""
2324

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
2627

2728
fun validate(): Boolean {
28-
if(isValid) {
29-
successListener?.invoke()
29+
if (isValid) {
30+
validListener?.invoke()
3031
} else {
31-
errorListener?.invoke(errorMessage)
32+
invalidListener?.invoke(errorMessage)
3233
}
3334

3435
return isValid
3536
}
3637

3738
fun required(): InputValidator {
38-
if(text.isEmpty()) {
39+
if (text.isEmpty()) {
3940
setErrorMessage("Required field.")
4041
}
4142
return this
4243
}
4344

4445
fun required(regex: String, error: String = "Invalid input"): InputValidator {
45-
if(!text.matches(Regex(regex))) {
46+
if (!text.matches(Regex(regex))) {
4647
setErrorMessage(error)
4748
}
4849
return this
4950
}
5051

5152
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))) {
5455
setErrorMessage(error)
5556
}
5657
return this
@@ -62,30 +63,179 @@ class InputValidator(val text: String) {
6263
return this
6364
}
6465

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+
65210
fun minimumLength(length: Int): InputValidator {
66211
minimumLength = length
67-
if(isOptional()) return this
68-
if(text.length < minimumLength) {
212+
if (isOptional()) return this
213+
if (text.length < minimumLength) {
69214
setErrorMessage("Minimum length required: $minimumLength")
70215
}
71216
return this
72217
}
73218

74219
fun maximumLength(length: Int): InputValidator {
75220
maximumLength = length
76-
if(text.length > maximumLength) {
221+
if (text.length > maximumLength) {
77222
setErrorMessage("Maximum length allowed: $maximumLength")
78223
}
79224
return this
80225
}
81226

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
84234
return this
85235
}
86236

87-
fun setErrorListener(errorListener: (error: String) -> Unit): InputValidator {
88-
this.errorListener = errorListener
237+
fun setCustomErrorMessage(error: String): InputValidator {
238+
errorMessage = error
89239
return this
90240
}
91241

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.arbazmateen.validator
2+
3+
const val REGEX_EMAIL =
4+
"^[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{2,256}\\@[a-zA-Z0-9\\-]{2,64}(\\.[a-zA-Z\\-]{2,25})*\\.[a-zA-Z\\-]{2,25}\$"
5+
6+
const val REGEX_PASSWORD =
7+
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$@!~%_\\-+=`',/:;\\\\< >{|}#\\^*?&()\\]\\[.\"]).{6,}\$"
8+
9+
const val REGEX_PASSWORD_WO_SPACE =
10+
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[$@!~%_\\-+=`',/:;\\\\<>{|}#\\^*?&()\\]\\[.\"])(?=\\S+\$).{6,}\$"
11+
12+
var REGEX_PASSWORD_CUSTOM_LENGTH: String = REGEX_PASSWORD
13+
set(length) {
14+
field.replace("{6,}", "{$length}")
15+
}
16+
17+
var REGEX_PASSWORD_WO_SPACE_CUSTOM_LENGTH: String = REGEX_PASSWORD_WO_SPACE
18+
set(length) {
19+
field.replace("{6,}", "{$length}")
20+
}
21+
22+
const val REGEX_LOWER_CASE_CHARACTERS = "^[a-z]+\$"
23+
const val REGEX_UPPER_CASE_CHARACTERS = "^[A-Z]+\$"
24+
const val REGEX_CHARACTERS = "^[a-zA-Z]+\$"
25+
const val REGEX_CHARACTERS_W_SPACE = "^[a-zA-Z ]+\$"
26+
const val REGEX_CHARACTERS_W_SPACE_HYPHEN = "^[a-zA-Z -]+\$"
27+
const val REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT = "^[a-zA-Z -.]+\$"
28+
const val REGEX_CHARACTERS_W_SPACE_HYPHEN_DOT__ = "^[a-zA-Z -._]+\$"
29+
30+
const val REGEX_ALPHA_NUMERIC = "^[a-zA-Z0-9]+\$"
31+
const val REGEX_ALPHA_NUMERIC_W_SPACE = "^[a-zA-Z0-9 ]+\$"
32+
const val REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN = "^[a-zA-Z0-9 -]+\$"
33+
const val REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN_DOT = "^[a-zA-Z0-9 -.]+\$"
34+
const val REGEX_ALPHA_NUMERIC_W_SPACE_HYPHEN_DOT__ = "^[a-zA-Z0-9 -._]+\$"
35+
36+
const val REGEX_NUMBERS = "^-?[1-9]+[0-9]*\$|^[0-9]+\$"
37+
const val REGEX_POSITIVE_NUMBERS = "^[1-9]\\d*\$"
38+
const val REGEX_NEGATIVE_NUMBERS = "^-[1-9]\\d*\$"
39+
40+
const val REGEX_PAK_LL_NUMBER = "^0\\d{2}[- ]?\\d{7}\$|^\\d{7}\$"
41+
const val REGEX_PAK_CELL_NUMBER = "^((\\+92)|(0092))[- ]?3\\d{2}[- ]?\\d{7}\$|^03\\d{2}[- ]?\\d{7}\$"
42+
43+
const val REGEX_PAK_CNIC = "^\\d{5}[- ]?\\d{7}[- ]?\\d\$"
44+
const val REGEX_PAK_NTN = "^\\d{7}[- ]?\\d\$"
45+
46+
const val REGEX_IBAN = "^[a-zA-Z]{2}\\d{2} ?[a-zA-Z]{4} ?\\d{4} ?\\d{4} ?\\d{4} ?\\d{4}\$"
47+
48+
const val REGEX_TWO_FLOATING_POINT =
49+
"^-?0\\.([1-9][0-9]|[0-9][1-9])\$|^-?[1-9]*[0-9]*(\\.[0-9]{0,2})?\$"
50+
51+
const val REGEX_THREE_FLOATING_POINT =
52+
"^-?0\\.([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])\$|^-?[1-9]*[0-9]*(\\.[0-9]{0,2})?\$"
53+
54+
const val REGEX_POSITIVE_TWO_FLOATING_POINT =
55+
"^0\$|^0\\.([1-9][0-9]|[0-9][1-9])\$|^[1-9]*[0-9]*(\\.[0-9]{0,2})?\$"
56+
57+
const val REGEX_POSITIVE_THREE_FLOATING_POINT =
58+
"^0\$|^0\\.([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])\$|^[1-9]*[0-9]*(\\.[0-9]{0,3})?\$"

0 commit comments

Comments
 (0)