-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.php
More file actions
167 lines (154 loc) · 5.59 KB
/
validate.php
File metadata and controls
167 lines (154 loc) · 5.59 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Validation Result</title>
</head>
<body>
<h1>Validation Result</h1>
<?php
$name = $email = $date = $dateInput = $time = $timeInput = $datetimeInput = $datetimeLocalInput = $month = $week = $number = $range = $color = ""; //initialize variable input
$invalidInputs = 0; //number of invalid inputs
$invalidInputFormats = 0; //number of invalid input formats
$nameErr = $emailErr = $dateErr = $dateInputErr = $timeErr = $timeInputErr = $datetimeInputErr = $datetimeLocalInputErr = $monthErr = $weekErr = $numberErr = $rangeErr = $colorErr = ""; //initialize error variables
if ($_SERVER["REQUEST_METHOD"] == "POST") { //check if form is submitted using POST method
if (empty($_POST["name"])) { //if input value is empty
$nameErr = "Name is required"; //set error message
$invalidInputs++; //increase invalid input count
} else {
$name = htmlspecialchars($_POST["name"]); //display entered input value
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$invalidInputs++;
} elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { //if input value is not valid format
$emailErr = "Invalid email format"; //set error message
$invalidInputFormats++; //increase invalid input format count
} else {
$email = htmlspecialchars($_POST["email"]);
}
if (empty($_POST["date"])) {
$dateErr = "Date is required";
$invalidInputs++;
} else {
$date = htmlspecialchars($_POST["date"]);
$dateTimestamp = strtotime($dateInput);
if ($dateTimestamp === false) {
$dateInputErr = "Invalid date format";
$invalidInputFormats++;
}
}
if (empty($_POST["time"])) {
$timeErr = "Time is required";
$invalidInputs++;
} else {
$time = htmlspecialchars($_POST["time"]);
$timeTimestamp = strtotime($timeInput);
if ($timeTimestamp === false) {
$timeInputErr = "Invalid time format";
$invalidInputFormats++;
}
}
if (empty($_POST["datetimeInput"])) {
$datetimeInputErr = "Datetime Input is required";
$invalidInputs++;
} else {
$datetimeInput = htmlspecialchars($_POST["datetimeInput"]);
}
if (empty($_POST["datetimeLocalInput"])) {
$datetimeLocalInputErr = "Datetime Local Input is required";
$invalidInputs++;
} else {
$datetimeLocalInput = htmlspecialchars($_POST["datetimeLocalInput"]);
}
if (empty($_POST["month"])) {
$monthErr = "Month is required";
$invalidInputs++;
} else {
$month = htmlspecialchars($_POST["month"]);
}
if (empty($_POST["week"])) {
$weekErr = "Week is required";
$invalidInputs++;
} else {
$week = htmlspecialchars($_POST["week"]);
}
if (empty($_POST["number"])) {
$numberErr = "Number is required";
$invalidInputs++;
} else {
$number = htmlspecialchars($_POST["number"]);
}
if (empty($_POST["range"])) {
$rangeErr = "Range is required";
$invalidInputs++;
} else {
$range = htmlspecialchars($_POST["range"]);
}
if (empty($_POST["color"])) {
$colorErr = "Color is required";
$invalidInputs++;
} else {
$color = htmlspecialchars($_POST["color"]);
}
}
if ($invalidInputs > 0) {
echo "<p style='color:red;'>Invalid inputs: $invalidInputs"; //display number of invalid inputs
}
if ($invalidInputFormats > 0) {
echo "<p style='color:red;'>Invalid input formats: $invalidInputFormats"; //display number of invalid input formats
}
echo "<p>Name: $name</p>"; //display entered input value
if ($nameErr) {
echo "<p style='color:red;'>$nameErr</p>"; //display error message
}
echo "<p>Email: $email</p>";
if ($emailErr) {
echo "<p style='color:red;'>$emailErr</p>";
}
echo "<p>Date: $date</p>";
if ($dateErr) {
echo "<p style='color:red;'>$dateErr</p>";
}
echo "<p>Date Input: $dateInput</p>";
if ($dateInputErr) {
echo "<p style='color:red;'>$dateInputErr</p>";
}
echo "<p>Time: $time</p>";
if ($timeErr) {
echo "<p style='color:red;'>$timeErr</p>";
}
echo "<p>Time Input: $timeInput</p>";
if ($timeInputErr) {
echo "<p style='color:red;'>$timeInputErr</p>";
}
echo "<p>Datetime Input: $datetimeInput</p>";
if ($datetimeInputErr) {
echo "<p style='color:red;'>$datetimeInputErr</p>";
}
echo "<p>Datetime Local Input: $datetimeLocalInput</p>";
if ($datetimeLocalInputErr) {
echo "<p style='color:red;'>$datetimeLocalInputErr</p>";
}
echo "<p>Month: $month</p>";
if ($monthErr) {
echo "<p style='color:red;'>$monthErr</p>";
}
echo "<p>Week: $week</p>";
if ($weekErr) {
echo "<p style='color:red;'>$weekErr</p>";
}
echo "<p>Number: $number</p>";
if ($numberErr) {
echo "<p style='color:red;'>$numberErr</p>";
}
echo "<p>Range: $range</p>";
if ($rangeErr) {
echo "<p style='color:red;'>$rangeErr</p>";
}
echo "<p>Color: $color</p>";
if ($colorErr) {
echo "<p style='color:red;'>$colorErr</p>";
}
?>
</body>
</html>