-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_functions.php
108 lines (55 loc) · 1.62 KB
/
validation_functions.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
<?php
function field_name_as_text($field_value){
$field_value = ucfirst($field_value);
$field_value = str_replace("_", " ", $field_value);
return $field_value;
}
$empty = array();
//* For Presence
function has_presence($value){
return isset($value) && $value !=="";
}
//* Validating array of presence
function validate_presence($has_presence){
Global $errors;
foreach($has_presence as $field ){
$value = trim($_POST[$field]);
if(!has_presence($value)){
$errors[$field] = field_name_as_text($field)." can not be Blank ";
};
}
}
//* For Max Length
function max_length($string,$max){
return strlen($string)<=$max;
}
//* Validating Max Lengths
function validate_max_length($field_with_max_length){
Global $errors;
foreach($field_with_max_length as $field => $max){
$value = trim($_POST[$field]);
if(!max_length($value,$max)){
$errors[$field] = field_name_as_text($field)." is Too Long ";
};
}
}
//* For Inclusion
function has_inclusion_in($value,$set){
return in_array($value,$set);
}
//* Displaying Errors
function form_errors($errors=array()){
$output ="";
if(!empty($errors))
{ $output .= "<center><div class= \"error\">";
$output .= "Please Fix Following Errors ";
$output .="<ul>";
foreach($errors as $key => $error){
$output .= "<li>{$error}</li>";
}
$output .="</ul>";
$output .="</div></center>";
}
return $output;
}
?>