-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_validator.php
215 lines (184 loc) · 5.16 KB
/
input_validator.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
208
209
210
211
212
213
214
215
<?php
/**
* get the post variables from form input
*/
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$street_address = $_POST['street_address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
/**
* this file contains 3 arrays
* $us_states : all the US state abbriviations and names in a 'key' => 'value' format
* $canadian_provences : all the Canadian provence abbriviations and names in a 'key' => 'value' format
* $state_array : this merges both arrays above
*/
require_once('state_list.php');
/**
* this file contains 1 array
* $ZIPREG : regular expressions for zip codes for the countries listed below
* US,UK,DE,CA,FR,IT,AU,NL,ES,DK,SE,BE
*/
require_once('zip_reg.php');
require_once('functions.php');
/*/
*********************************************************************************************************************
* Start Validation
********************************************************************************************************************
**/
if($_POST['submited']){
// run validation for first_name
$error_message .= validateString(array(
'string' => $first_name,
'label' => 'First Name',
'min' => 2,
'max' => 20,
'allowed' => "/[^A-Za-z-' ]/i",
'message' => 'should only contain letters, hyphens, apostrophies, or spaces'
)
);
// run validation for last_name
$error_message .= validateString(array(
'string' => $last_name,
'label' => 'Last Name',
'min' => 2,
'max' => 20,
'allowed' => "/[^A-Za-z-' ]/i",
'message' => 'should only contain letters, hyphens, apostrophies, or spaces'
)
);
// run validation for address
$error_message .= validateString(array(
'string' => $street_address,
'label' => 'Address',
'min_letters' => 2,
'min' => 3,
'max' => 80,
'allowed' => "/[^A-Za-z0-9\-#.' ]/i",
'message' => 'should contain only letters, numbers, the # sign, apostrophes, hyphens, periods and spaces'
)
);
// run validation for city
$error_message .= validateString(array(
'string' => $city,
'label' => 'City',
'min' => 3,
'max' => 25,
'allowed' => "/[^A-Za-z-' ]/i",
'message' => 'should contain only letters, hyphens, apostrophes, or spaces'
)
);
// run validation for state
$error_message .= validateString(array(
'string' => $state,
'label' => 'State',
'min' => 2,
'max' => 2,
'array_key' => $us_states
)
);
// run validation for zip code
$error_message .= validateString(array(
'string' => $zip,
'label' => 'Zip Code',
'min' => 5,
'max' => 10,
'match_format' => '/'.$ZIPREG['US'].'/i',
'message' => 'you entered an invalid US zip code. It should be in this format (33321) or (33312-1234)'
)
);
// run validation for phone
$error_message .= validateString(array(
'string' => $phone,
'label' => 'Phone Number',
'min' => 10,
'max' => 12,
'min_numbers' => 10,
'allowed' => "/[^0-9\-' ]/i",
'message' => 'should contain only numbers and hyphens (ex: 555-123-4567)'
)
);
}
/*/
*********************************************************************************************************************
* End Validation
********************************************************************************************************************
**/
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Input Validator</title>
</head>
<body>
<?php
if($_POST['submited']){
echo success_message($success_message);
echo error_message($error_message);
}
?>
<h1>Input Validator</h1>
<form method="post" action="<?php echo getenv('SCRIPT_NAME'); //this just post the form to it's self ?>">
<input type="hidden" name="submited" value="1">
<!-- first name -->
<div >
<label for="first_name" >First Name</label>
<div >
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>" >
</div>
</div>
<!-- last name -->
<div >
<label for="last_name" >Last Name</label>
<div >
<input type="text" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>" >
</div>
</div>
<!-- address -->
<div >
<label for="street_address" >Address</label>
<div >
<input type="text" name="street_address" placeholder="123 Penny Lane" value="<?php echo $street_address; ?>" >
</div>
</div>
<!-- city -->
<div >
<label for="city" >City</label>
<div >
<input type="text" name="city" placeholder="Some City" value="<?php echo $city; ?>" >
</div>
</div>
<!-- state -->
<div >
<label for="state" >State</label>
<div >
<select class="form-control" name="state" required>
<?php echo option_list($us_states , 'state'); //create a dropdown list from the states array ?>
</select>
</div>
</div>
<!-- zip -->
<div >
<label for="zip">Zip</label>
<div>
<input type="text" name="zip" placeholder="32165" value="<?php echo $zip; ?>" >
</div>
</div>
<!-- phone -->
<div >
<label for="phone" >Phone</label>
<div >
<input type="text" name="phone" placeholder="954-545-5468" value="<?php echo $phone; ?>" >
</div>
</div>
<div>
<div >
<button type="submit">Submit</button>
</div>
</div>
</form>
</body>
</html>