-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.php
52 lines (46 loc) · 1.4 KB
/
validate.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
<?php
$name = $email = $issue = $message = "";
$namerr = $emailerr = $issuerr = $messagerr = "";
// Validate the Name field
if (empty($_POST['name'])) {
$namerr = "Please enter your name";
} elseif (strlen($_POST['name']) < 3) {
$namerr = "Your name must be at least three characters long.";
} else {
$name = val_input($_POST['name']);
if (!preg_match("/^[a-zA-Z' ]*$/", $name)) {
$namerr = "Your name can contain only letters and white spaces.";
}
}
// Validate the email field
if (empty($_POST['email'])) {
$emailerr = "Please enter your email.";
} else {
$email = val_input($_POST['email']);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailerr = "Invalid email format";
}
}
// Validata the issue field
if ( empty( $_POST['issue'] ) ) {
$issuerr = "Please select an issue.";
} else {
$issue = val_input($_POST["issue"]);
}
// Validate the message field
if (empty($_POST['message'])) {
$messagerr = "Please explain your issue with us.";
} elseif (strlen($_POST['message']) < 30) {
$messagerr = "Your message must be atlease 30 characters long.";
} else {
$message = val_input($_POST['message']);
}
function val_input($data) {
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$successful = $name && $email && $issue && $message;
$error = $namerr || $emailerr || $issuerr || $messagerr;
?>