Hier bij een eerste versie van FormChecker.
De mogelijkheden :
EMAIL, IPV4, TIME, DATE, DATETIME, INT, STRING, DUTCHPOSTCODE, ALFABET, USERNAME
To Do :
DUTCHPHONE
FTP (geldige ftp url)
Beter foutmeldingen.
De mogelijkheden :
EMAIL, IPV4, TIME, DATE, DATETIME, INT, STRING, DUTCHPOSTCODE, ALFABET, USERNAME
To Do :
DUTCHPHONE
FTP (geldige ftp url)
Beter foutmeldingen.
edit:
Deze code functioneerd volledig !:)
Dit is een klein cadeautje van mij, omdat enkele mensen hier mij hebben geholpen met het begrijpen en schrijven reguliere expressies.
Hij wordt nog verder uitgewerkt en ik beloof dat het allemaal opensource zal blijven. Ik sta open voor zinnige bijdragen en opmerkingen.
Deze code functioneerd volledig !:)
Dit is een klein cadeautje van mij, omdat enkele mensen hier mij hebben geholpen met het begrijpen en schrijven reguliere expressies.
Hij wordt nog verder uitgewerkt en ik beloof dat het allemaal opensource zal blijven. Ik sta open voor zinnige bijdragen en opmerkingen.
code:
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
| <?
// FormChecker V0.5 VGouw
class FormChecker{
// Vars which contains the last special error message like EMPTY LENGHT> LENGTH< VALUE> VALUE<
var $errorMsg;
function FormFieldChecker(){
$this -> errorMsg = '';
}
function getErrorMessage(){
return $this -> errorMsg;
}
/**
* Checks if the given url matches de description of a url.
* If the url is no started with http:// it is added before the check.
* Url has to start with http://, followed by something with length at least 3 that
* contains at least one dot.
**/
function isURL($url){
$isUrl = FALSE;
// If the url does not start 'with http://' add it
if (!preg_match('#^http:\/\/#i', $url) ){
$url = 'http://' . $url;
}
if (preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) ){
$isURL = TRUE;
}
return $isURL;
}
/**
* Checks to see if the given numberString only contains numbers
**/
function isOnlyNumbers($numberString) {
$isOnlyNumbers = FALSE;
if ( preg_match('/^[0-9]+$/', $numberString) ){
$isOnlyNumbers = TRUE;
}
return $isOnlyNumbers;
}
/**
* Checks if the email is a valide one
**/
function isEmailAdress($email){
$isEmailAdress = FALSE;
if ( preg_match('/^[a-z0-9\.\-_]+@[a-z0-9\-_]+\.([a-z0-9\-_]+\.)*?[a-z]+$/is', $email) ){
$isEmailAdress = TRUE;
}
return $isEmailAdress;
}
/**
* Checks if an $IP is valid, according to 192.168.168.20
**/
function isIPV4($Ip)
{
$isIP = FALSE;
if(!preg_match('!^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$!', $Ip) === FALSE){
$isIP = TRUE;
}
return $isIP;
}
*/
/**
* Check to see if the $time is a valid time. Seperator kan be everything but number and as many as possible
* 23fdfdfas12fsdfsdfsdf6 is a valid time
**/
function isTime($time){
$isTime = FALSE;
$parts = split('[^[:digit:]]+',$time) ;
if (count($parts) === 3){
if ( $parts[0] >= 0 && $parts[0] <= 24
&& $parts[1] >= 0 && $parts[1] <= 59
&& $parts[2] >= 0 && $parts[2] <= 59 ){
$isTime = TRUE;
}
}
return $isTime;
}
/**
* Checks is a date if valid. Seperator can be everything except numbers.
* The given date must complie with the format : day , month, year
**/
function isDate($date){
$isDate = FALSE;
$parts = split('[^[:digit:]]+',$date) ;
if (count($parts) === 3){
$isDate = checkdate($parts[1],$parts[0],$parts[2]);
}
return $isDate;
}
/**
* Checks is a dateTime if valid. Seperator can be everything except numbers.
* The given date must complie with the format : day , month, year, hour,minute,second
**/
function isDateTime($dateTime){
$isDateTime = FALSE;
$parts = split('[^[:digit:]]+',$dateTime);
if (count($parts === 6)){
if ($this -> isDate($parts[0].'-'.$parts[1].'-'.$parts[2]) && $this -> isTime($parts[3].':'.$parts[4].':'.$parts[5]) ){
$isDateTime = TRUE;
}
}
return $isDateTime;
}
function isDutchPostcode($postcode){
$isDutchPostcode = FALSE;
if (preg_match('/^[1-9]{1}[0-9]{3} ?[a-zA-Z]{2}$/',$postcode))
$isDutchPostcode = TRUE;
}
return $isDutchPostcode;
}
// TO DO
function isDuthcPhoneNumber($phoneNumber){
$isDuthcPhoneNumber = FALSE;
// TO DO
return $isDuthcPhoneNumber;
}
/**
* Checks if a username is of the correct format 4-20 containing Leters, numbers or _
**/
function isUserName($userName){
$isUserName = FALSE;
echo($userName);
if ($userName != ''){
if(preg_match("/^[-A-Za-z0-9_]+$/",$userName)){
$isUserName = TRUE;
}
}
return $isUserName;
}
function isOnlyAlfabet($value){
$isOnlyAlfabet = FALSE;
if ($value != ''){
if (preg_match ("/^[a-zA-Z]+$/",$value)){
$isOnlyAlfabet = TRUE;
}
}
return $isOnlyAlfabet;
}
/**
* Function which checks the form fields, according to their types.
* Supported are : EMAIL, IPV4, TIME, DATE, DATETIME, INT, STRING, DUTCHPOSTCODE, ALFABET, USERNAME
*
*/
function isValidFormValue($value,$type = 'STRING',$min = FALSE, $max = FALSE, $trimChop = TRUE){
$type = strtoupper($type);
$errorMsg = '';
// Chop and Trim
if ($trimChop === TRUE){
$value = trim(chop($value));
}
// Checking is value not is empty
if ($value == ''){
$this -> errorMsg = 'EMPTY';
return FALSE;
}
// Checks for the types STRING, EMAIL, URL, USERNAME,ALFABET if the lenght is within margines, if given.
if ( $type == 'STRING'
|| $type == 'EMAIL'
|| $type == 'URL'
|| $type == 'ALFABET'
|| $type == 'USERNAME' ){
if ($min != FALSE && strlen($value) < $min){
$this -> errorMsg = 'LENGTH<'.$min;
return FALSE;
}
if ($max !== FALSE && strlen($value) > $max){
$this -> errorMsg = 'LENGTH>'.$max;
return FALSE;
}
}
// Checks for the different types
if ($type === 'EMAIL'){
return $this -> isEmailAdress($value);
} else if ($type === 'ALFABET'){
return $this -> isOnlyAlfabet($value);
}else if ($type === 'DUTCHPOSTCODE'){
return $this -> isDutchPostcode($value);
} else if ($type === 'URL'){
return $this -> isUrl($value);
} else if ($type === 'USERNAME'){
return $this -> isUserName($value);
} else if ($type === 'TIME') {
return $this -> isTime($value);
} else if ($type === 'DATE') {
return $this -> isDate($value);
} else if ($type === 'DATETIME') {
return $this -> isDateTime($value);
} else if ($type === 'IPV4') {
return $this -> isIPV4($value);
}else if ($type === 'INT') {
$isOnlyNumbers = FALSE;
if ($this -> isOnlyNumbers($value)){
$isOnlyNumbers = TRUE;
if ($min !== FALSE && $this -> isOnlyNumbers($min) && $value >= $min){
$this -> errorMsg = 'VALUE<'.$mix;
$isOnlyNumbers = TRUE;
}
if ($max !== FALSE && $this -> isOnlyNumbers($max) && $value <= $max){
$this -> errorMsg = 'VALUE>'.$max;
$isOnlyNumbers = TRUE;
}
}
return $isOnlyNumbers;
} else if ($type === 'STRING') {
$isString = TRUE;
return $isString;
}
return FALSE;
}
}
?> |
edit:
Deze code functioneerd volledig !:)
Deze code functioneerd volledig !:)