Report abuse

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
 * Validation class
 * Contains a number of validation methods to keep validation to one class
 * When using, (i.e. varEmpty), you would need to check for the return being null to pass validation, i.e.
 * if($validate->varEmpty($var, 'key name') == null):
 * // validation passed. do something.
 * else:
 * // do something else
 * endif;
 * @author Pete Robinson - www.pete-robinson.co.uk
 */

class Validate
{
	
	public $errors = array();
		
	/**
	 * Check that a numeric value falls between two other numberic values
	 * @param $min - int - the minimum value that $value must be
	 * @param $max - int - the maximum value that $value must be
	 * @param $value - the value to be checked
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function between($min, $max, $value, $key='')
	{
		if($value < $min || $value > $max) {
			$this->errors[] = $key . ' must be between ' . $min . ' and ' . $max . '.';
			return false;
		}
	}
	
	
	/**
	 * Check to see if a variable holds data
	 * @param $value - the variable to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function varEmpty($value, $key='')
	{
		if(!$value || $value == '' || empty($value)) {
			$this->errors[] = $key . ' must have a value.';
			return false;
		}
	}
	
	
	/**
	 * Check that an email address follows a valid email address format
	 * @param $value - str - the email address to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function email($value, $key='')
	{
		if(!preg_match('/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i', $value)) {
			$this->errors[] = $key . ' must be a valid e-mail address.';
			return false;
		}
	}
	
	
	/**
	 * Compare two values
	 * @param $value1 - str - the first value
	 * @param $value2 - str - the second value. Must match $value1 to pass validation
	 * @param $key1 - str - the variable name to be presented to the user in error report
	 * @param $key2 - str - the variable name to be presented to the user in error report
	 */
	public function equal($value1, $value2, $key1='', $key2='')
	{
		if($value1 != $value2) {
			$this->errors[] = $key1 . ' must be equal to ' . $key2 . '.';
			return false;
		}
	}
	
	
	/**
	 * Check whether a value is numeric
	 * @param $value - the number to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function num($value, $key='')
	{
		if(!ereg('^[[:digit:]]+$', $value)) {
			if($key) {
				$this->errors[] = $key . ' must be a numeric value.';
			}
			return false;
		}
	}
	
	
	
	/**
	 * Compare a list of required fields against an array
	 * @param $required - arr - the array of keys
	 * @param $fields - arr - the array of values
	 */
	public function requiredFields($required, $fields)
	{
		$results = array();
		$i = 0;
		foreach($required as $req) {
			if($this->varEmpty($fields[$req], $req) === false) {
				$results[$i] = $req;
				$i++;
			}
		}
		
		if($results) {
			return false;
		}
	}
	
	
	/**
	 * Check to see if an array holds one or more keys
	 * @param $arr - arr - the array to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function arrayData($arr, $key='')
	{
		if(count($arr) == 0 || !is_array($arr)) {
			$this->errors[] = $key . ' must be an array.';
			return false;
		}
	}
	
	
	/**
	 * Check to see if a value is alphanumeric
	 * @param $value - str - the value to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function alphaNum($value, $key='')
	{
		if(!preg_match('/^[a-zA-Z0-9]+$/', $value)) {
			$this->errors[] = $key . ' must be an alpha-numeric value.';
			return false;
		}
	}
	
	
	/**
	 * Check whether a given value is a valid credit card number.
	 * @param $number - int - the 16 digit credit card number
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function creditCardNo($number, $key='')
	{
		if(strlen($number) != 16) {
			$this->errors[] = $key . ' is not a valid credit card number.';
			return false;
		} else {
			if($this->num($number) === false) {
				$this->errors[] = $key . ' is not a valid credit card number.';
				return false;
			}
		}
	}
	
	
	/**
	 * Check to ensure that a credit card start date is in the past
	 * @param $month - int - the 2 digit month to be validated (i.e. 02)
	 * @param $year - int - the 2 digit year to be validated (i.e. 09)
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function creditCardStart($month, $year, $key='')
	{
		if($this->num($month) == null && $this->num($year) == null) {
			if($this->pastDate(($year+2000) . '-' . $month . '-01') === false) {
				$this->errors[] = $key . ' must be in the past.';
				return false;
			}
		} else {
			$this->errors[] = 'Month and year must be in numeric format.';
			return false;
		}
	}
	
	
	/**
	 * Check to ensure that a credit card expiry date is in the future
	 * @param $month - int - the 2 digit month to be validated (i.e. 02)
	 * @param $year - int - the 2 digit year to be validated (i.e. 09)
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function creditCardExpire($month, $year, $key='')
	{
		if($this->num($month) == null && $this->num($year) == null) {
			if($this->futureDate(($year + 2000) . '-' . $month . '-' . cal_days_in_month(CAL_GREGORIAN, $month, ($year + 2000))) === false) {
				$this->errors[] = $key . ' must be a date in the future.';
				return false;
			}
		} else {
			$this->errors[] = 'Month and year must be in numeric format';
			return false;
		}
	}
	
	
	/**
	 * Ensure a date (in Y-m-d format) is in the past
	 * @param $value - str - the date to be checked
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function pastDate($value, $key='')
	{
		if($value > date('Y-m-d')) {
			if($key) {
				$this->errors[] = $key . ' must be a date in the past.';
			}
			return false;
		}
	}
	
	
	/**
	 * Ensure that a date (Y-m-d format) is in the future
	 * @param $value - str - the date to be validated
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function futureDate($value, $key='')
	{
		if($value < date('Y-m-d')) {
			if($key) {
				$this->errors[] = $key . ' must be in the past.';
			}
			return false;
		}
	}
	
	
	/**
	 * Check that a date falls between two other dates
	 * @param $date - str - the subject of the method
	 * @param $from - str - the lower limit of the date range
	 * @param $to - str - the upper limit of the date range
	 * @param $key - str - the variable name to be presented to the user in error report
	 */
	public function dateBetween($date, $from, $to, $key='')
	{
		if(($date < $from) || ($date > $to)) {
			if($key) {
				$this->errors[] = $key . ' must be between ' . $from . ' and ' . $to . '.';
			}
			return false;
		}
	}
	
	
	/**
	 * Check that a file is in a valid document format (PDF, DOC, DOCX, XSL, XLSX)
	 * And that is has a max size of 10MB and has no errors
	 * Add your own mime types to the array if needed
	 * @param $file - arr - the $_FILES array of the file
	 */
	public function validateFile($file)
	{
		if($file['size'] >= 102400000) {
			$this->errors[] = 'File must be smaller than 10MB';
			return false;
		}
		
		if(!in_array($file['type'], array('application/pdf', 'application/msword', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'))) {
			$this->errors[] = 'File must be in pdf, doc, xls, docx or xlsx format.';
			return false;
		}
		
		if($file['error'] > 0) {
			$this->errors[] = 'Error in uploaded file.';
			return false;
		}
	}
	
	
	
}



?>