-
Notifications
You must be signed in to change notification settings - Fork 14
/
cf7-custom-validation.php
412 lines (321 loc) · 11.4 KB
/
cf7-custom-validation.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
File: Contact Form 7 Custom Validation
Author: Tirumal
Website: http://www.code-tricks.com
Description: This file, when included in functions.php of WordPress theme extends the contact form 7 data validations.
Such as date, email, phone number, zip codes etc. You can also extend the validations just by providing new names and
respective validaton rules either conditional or reg expressive.
Please note that this is not a plugin. You need to include this file in your theme's functions.php using PHP include()
function.
include("cf7-custom-validation.php");
More information on this post: http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/
*/
//url regex
function get_valid_url( $url ) {
$regex = "((https?|ftp)\:\/\/)?"; // Scheme
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
$regex .= "(\:[0-9]{2,5})?"; // Port
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor
return preg_match("/^$regex$/", $url);
}
// CF7 Custom Validation Rules
function compare_date($date1, $date2) {
$date1 = explode('/', $date1);
$date2 = explode('/', $date2);
$output = false;
if($date2[2] > $date1[2]) {
$output = true;
} else {
$output = false;
}
if($date2[2] == $date1[2]) {
if($date2[0] > $date1[0]) {
$output = true;
} else {
$output = false;
}
if($date2[0] == $date1[0]) {
if($date2[1] >= $date1[1]) {
$output = true;
} else {
$output = false;
}
}
}
if ($output) {
return true;
} else {
return false;
}
}
function cf7_custom_form_validation($result,$tag) {
$type = $tag['type'];
$name = $tag['name'];
if($type == 'text*' && $_POST[$name] == ''){
$result['valid'] = false;
$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
}
//__________________________________________________________________________________________________
//Comparision date
$date1 = $_POST['date1'];
$date2 = $_POST['date2'];
//CheckInDate
if($name == 'date1'){
if(!compare_date($date1, $date2)) {
$result['valid'] = false;
$result['reason'][$name] = 'Logical Error: Check in date should be before check out date';
}
}
//__________________________________________________________________________________________________
//CheckOutDate
if($name == 'date2'){
if(!compare_date($date1, $date2)) {
$result['valid'] = false;
$result['reason'][$name] = 'Logical Error: Check out date should be After check in date';
}
}
//__________________________________________________________________________________________________
//url
if($name == 'url') {
$url = $_POST['url'];
if($url != '') {
if(get_valid_url($url)){
$result['valid'] = true;
} else {
$result['valid'] = false;
$result['reason'][$name] = 'Entered URL is invalid.';
}
}
}
//__________________________________________________________________________________________________
//emailAddress
if($name == 'emailAddress') {
$emailAddress = $_POST['emailAddress'];
if($emailAddress != '') {
if(substr($emailAddress, 0, 1) == '.' || !preg_match('/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i', $emailAddress)) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Email is Invalid.';
}
}
}
//__________________________________________________________________________________________________
//datemdy
if($name == 'datemdy'){
$datemdy = $_POST['datemdy'];
if($datemdy != '') {
if(!preg_match('/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/', $datemdy)) {
$result['valid'] = false;
$result['reason'][$name] = 'Enter Date in MM/DD/YYYY Format';
}
}
}
//__________________________________________________________________________________________________
//US Zip code validation
//USZipCode
if($name == 'USZipCode') {
$USZipCode = $_POST['USZipCode'];
if($USZipCode != '') {
// if(!preg_match('/^([0-9]{5})(-[0-9]{4})?$/i', $USZipCode)) {
if(!preg_match('/^\d{5}(-\d{4})?$/', $USZipCode)) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Zipcode is Invalid';
}
}
}
//__________________________________________________________________________________________________
// CANADA Zip code validation
//CANZipCode
if($name == 'CANZipCode') {
$CANZipCode = $_POST['CANZipCode'];
if($CANZipCode != '') {
if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $CANZipCode)) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Zipcode is Invalid';
}
}
}
//__________________________________________________________________________________________________
// US and CANADA Zip code validation
//USCANZipCode
if($name == 'USCANZipCode') {
$USCANZipCode = $_POST['USCANZipCode'];
if($USCANZipCode != '') {
if(!preg_match('/^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/', $USCANZipCode)) {
if(!preg_match('/^\d{5}(-\d{4})?$/', $USCANZipCode)) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Zipcode is Invalid';
}
}
}
}
//__________________________________________________________________________________________________
// Indian Postal code validation
//inPostalCode
if($name == 'inPostalCode') {
$inPostalCode = $_POST['inPostalCode'];
if($inPostalCode != '') {
if(!preg_match('/^[0-9]{6,6}$/', $inPostalCode)) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Pin code for India is Invalid';
}
}
}
//__________________________________________________________________________________________________
//This section updated on 22nd March 2013
// It will accept character, character + numeric value
// It will not accept special characters
//fullName
$allNames = array('fullName', 'fullName1');
foreach($allNames as $uniNames) {
if($name == $uniNames) {
$fullName = $_POST[$uniNames];
if($fullName != '') {
if(!preg_match('/^[A-Z0-9][a-zA-Z0-9 ]+$/i', $fullName)) {
$result['valid'] = false;
$result['reason'][$name] = 'Please Enter a Valid Name';
}
if(is_numeric($fullName)){
$result['valid'] = false;
$result['reason'][$name] = 'Please Enter a Valid Name';
}
}
}
}
//__________________________________________________________________________________________________
//acceptNum
$acceptNumbers = array('acceptNumber', 'acceptNumber1', 'acceptNumber2', 'acceptNumber3', 'acceptNumber4', 'acceptNumber5', 'acceptNumber6');
foreach($acceptNumbers as $acceptNumber){
if($name == $acceptNumber) {
$acceptNum = $_POST[$acceptNumber];
if($acceptNum != '') {
if(ctype_digit($acceptNum)) {
$result['valid'] = true;
} else {
$result['valid'] = false;
$result['reason'][$name] = 'Please Enter Only Numbers';
}
}
}
}
//__________________________________________________________________________________________________
//faxNumber
$faxNumbers = array('faxNumber', 'faxNumber1', 'faxNumber2', 'faxNumber3', 'faxNumber4', 'faxNumber5', 'faxNumber6');
foreach($faxNumbers as $faxNum) {
if($name == $faxNum) {
$faxNumber = $_POST[$faxNum];
$contRegex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
if ($faxNumber != '') {
if(!preg_match($contRegex, $faxNumber) && strlen($faxNumber) > 10 && strlen($faxNumber) < 18) {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Fax Number is Invalid';
}
}
}
}
//__________________________________________________________________________________________________
//Agreement fields with the expected values. Ex. write ,,yes" to agree
$agreement = array('agreement','agreement1','agreement2');
$expectedValues = array('yes', 'Yes', 'YES');
foreach ($agreement as $a => $aValue) {
if($name == $aValue) {
foreach ($expected_values as $i => $eValue) {
if($eValue == $_POST[$name]) {
$result['valid'] = true; break;
} else {
$result['valid'] = false;
$result['reason'][$name] = 'You did not agree with the terms';
}
}
}
}
//__________________________________________________________________________________________________
//Only Characters
$OnlyChars = array('onlyChar', 'onlyChar1', 'onlyChar2');
foreach($OnlyChars as $OnlyChar){
if($name == $OnlyChar) {
$onlyChar = $_POST[$OnlyChar];
if($onlyChar != '') {
$containsLettersOrNumbers = (preg_match('~[0-9a-z]~i', $onlyChar) > 0);
if(!$containsLettersOrNumbers
|| is_numeric($onlyChar)
|| strlen($onlyChar > 64) ) {
$result['valid'] = false;
$result['reason'][$name] = 'Please Enter Only Characters';
}
if(is_numeric($onlyChar)){
$result['valid'] = false;
$result['reason'][$name] = 'Please Enter Only Characters';
}
}
}
}
//__________________________________________________________________________________________________
//validPhone
/*
49-4312 / 777 777
+1 (305) 613-0958 x101
(305) 613 09 58 ext 101
3056130958
+33 1 47 37 62 24 extension 3
(016977) 1234
04312 - 777 777
91-12345-12345
+58 295416 7216
*/
$phoneNumbersAll = array('validPhone', 'validPhone1', 'validPhone2', 'validPhone3', 'validPhone4', 'validPhone5', 'validPhone6');
foreach($phoneNumbersAll as $validPhoneVal) {
if($name == $validPhoneVal) {
$validPhone = $_POST[$validPhoneVal];
if($validPhone != '') {
if(preg_match('/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/', $validPhone)
|| preg_match('/^([\+][0-9]{1,3}[\ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9\ \.\-\/]{3,20})((x|ext|extension)[\ ]?[0-9]{1,4})?$/', $validPhone)
&& strlen($validPhone) > 9
&& strlen($validPhone) < 30
&& (int)($validPhone)) {
//$result['valid'] = true;
} else {
$result['valid'] = false;
$result['reason'][$name] = 'Entered Phone Number is Invalid';
}
}
}
}
//__________________________________________________________________________________________________
return $result;
}
//add filter for text field validation
add_filter('wpcf7_validate_text','cf7_custom_form_validation', 10, 2); // text field
add_filter('wpcf7_validate_text*', 'cf7_custom_form_validation', 10, 2); // Req. text field
/*
TEXTAREA VALIDATIONS
*/
function cf7_custom_textarea_validation($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
//if empty give required field error
if($type == 'textarea*' && $_POST[$name] == ''){
$result['valid'] = false;
$result['reason'][$name] = wpcf7_get_message( 'invalid_required' );
}
//validations begins
//__________________________________________________________________________________________________
// Address text area validation
if($name == 'addressTa'){
$addressTa = $_POST['addressTa'];
if($addressTa != '') {
if(strlen($addressTa) > 300) {
$result['valid'] = false;
$result['reason'][$name] = 'You should not exceed 300 characters length';
}
}
}
return $result;
}
//add fiter for text area validation
add_filter( 'wpcf7_validate_textarea', 'cf7_custom_textarea_validation', 10, 2 );
add_filter( 'wpcf7_validate_textarea*', 'cf7_custom_textarea_validation', 10, 2 );
?>