-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidatedInput.java
109 lines (95 loc) · 3.32 KB
/
ValidatedInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package eyeclinic.Helpers;
import java.util.ArrayList;
/**
*
* @author dirchev
*/
public class ValidatedInput {
private Boolean valid = true;
private ArrayList<String> validationMessages = new ArrayList<>();
private String string;
private String label;
public ValidatedInput (String string, String label) {
this.string = string;
this.label = label;
}
private void checkCase (Boolean validationCase, String errorMessage) {
if (!validationCase) {
this.valid = false;
this.validationMessages.add(errorMessage.replace("*label*", "'" + label + "'"));
}
}
/**
* Checks if the string has at least 1 character
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput required () {
this.checkCase(string.length() != 0, "*label* is required.");
return this;
}
/**
* Checks if the string contains only upper or lower case letters or numbers
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput alphanum () {
this.checkCase(string.matches("^[a-zA-Z0-9]*$"), "*label* must include only letters and numbers.");
return this;
}
/**
* Checks if the string contains only upper or lower case letters
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput alpha () {
this.checkCase(string.matches("^[a-zA-Z]*$"), "*label* must include only letters.");
return this;
}
/**
* Checks if the string contains only numbers
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput integer () {
this.checkCase(string.matches("^[0-9]*$"), "*label* must be an integer.");
return this;
}
/**
* Checks if the string length is more than given length
* @param length length to be used in the validation
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput min_length (Integer length) {
this.checkCase(string.length() > length, "*label* must have more than " + length + " characters.");
return this;
}
/**
* Checks if the string length is less than the given length
* @param length length to be used in the validation
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput max_length (Integer length) {
this.checkCase(string.length() < length, "*label* must have less than " + length + " characters.");
return this;
}
/**
* Checks if the string is a valid email (beta)
* @return ValidatedInput instance with updated properties
*/
public ValidatedInput email () {
String emailRegex = "^[A-Z0-9._%+-]++@[A-Z0-9.-]++\\.[A-Z]{2,}+$";
this.checkCase(string.matches(emailRegex), "*label* must be valid email.");
return this;
}
/**
* Tells if the string meets the validations or not
* @return true if all validations have passed successfully and false otherwise
*/
public Boolean isValid() {
return valid;
}
/**
* Gives all validation error messages
* @return ArrayList of validation error messages
*/
public ArrayList<String> getValidationMessages() {
return validationMessages;
}
}