-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_form.php
executable file
·207 lines (143 loc) · 6.01 KB
/
validate_form.php
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
<?php
$form = new ProcessForm();
$form->field_rules = array(
'field3'=>'required',
'field4'=>'email|required',
'field5'=>'number|min[0]|max[10000000]|required',
'field6'=>'number|min[0]|max[1000000000]|required',
'field7'=>'number|min[0]|max[100000000]|required',
'field9'=>'required',
'field10'=>'number|min[0]|max[100]|required',
'field11'=>'number|min[1]|max[100]',
'field12'=>'number|min[0]|max[100000000]|required',
'field13'=>'number|min[0]|max[100000000]|required',
'field14'=>'number|min[0]|max[10000000]|required',
'field15'=>'number|min[0]|max[10000]|required',
'field16'=>'number|min[0]|max[1000]|required',
'field17'=>'number|min[0]|max[1000]|required',
'field18'=>'number|min[1]|max[1000]|required',
'field19'=>'number|min[0]|max[100]|required'
);
$form->validate();
class ProcessForm
{
public $field_rules;
public $error_messages;
public $fields;
private $error_list;
private $is_xhr;
function __construct()
{
$this->error_messages = array(
'required' => 'This field is required',
'email' => 'Please enter a valid email address',
'number' => 'Please enter a numeric value',
'url' => 'Please enter a valid URL',
'pattern' => 'Please correct this value',
'min' => 'Please enter a value larger than the minimum value',
'max' => 'Please enter a value smaller than the maximum value'
);
$this->field_rules = array();
$this->error_list = '';
$this->fields = $_POST;
$this->is_xhr = $this->xhr();
// echo json_encode($this->field_rules);
}
function validate()
{
if (!empty($this->fields))
{
//Validate each of the fields
foreach ($this->field_rules as $field => $rules)
{
$rules = explode('|', $rules);
foreach ($rules as $rule)
{
$result = null;
if (isset($this->fields[$field]))
{
$param = false;
if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
{
$rule = $match[1];
$param = $match[2];
}
$this->fields[$field] = $this->clean($this->fields[$field]);
//if the field is a checkbox group create string
if (is_array($this->fields[$field]))
$this->fields[$field] = implode(', ', $this->fields[$field]);
// Call the function that corresponds to the rule
if (!empty($rule))
$result = $this->$rule($this->fields[$field], $param);
// Handle errors
if ($result === false)
$this->set_error($field, $rule);
}
}
}
if (empty($this->error_list))
{
if ($this->is_xhr)
echo json_encode(array('status' => 'success'));
$this->process();
}
else
{
if ($this->is_xhr)
echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list));
else echo $this->error_list;
}
}
}
function set_error($field, $rule)
{
if ($this->is_xhr)
{
$this->error_list[$field] = $this->error_messages[$rule];
}
else $this->error_list .= "<div class='error'>$field: " . $this->error_messages[$rule] . "</div>";
}
function xhr()
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
}
/** Validation Functions */
function required($str, $val = false)
{
if (!is_array($str))
{
$str = trim($str);
return ($str == '') ? false : true;
}
else
{
return (!empty($str));
}
}
function email($str)
{
return (!preg_match("/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $str)) ? false : true;
}
function number($str)
{
return (!is_numeric($str)) ? false : true;
}
function min($str, $val)
{
return ($str >= $val) ? true : false;
}
function max($str, $val)
{
return ($str <= $val) ? true : false;
}
function pattern($str, $pattern)
{
return (!preg_match($pattern, $str)) ? false : true;
}
function clean($str)
{
$str = is_array($str) ? array_map(array("ProcessForm", 'clean'), $str) : str_replace('\\', '\\\\', strip_tags(trim(htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) : $str), ENT_QUOTES))));
return $str;
}
}
?>