So i have got this almost completely working. The one thing i am stuck on is where to start in preg_match. I have read the manual here http://php.net/manual/en/function.preg-match.php but it doesn't exactly describe to me the syntax for creating my own validation. Example: for a text area, if i wanna only allow white space, letters, numbers and maybe some special characters(unlikely). Here is the code
<?php
if(isset($_POST['name'])
&& isset($_POST['phone'])
&& isset($_POST['email'])) {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
else {
$name = $_POST["name"];
// check if name only contains letters and whitespace for name
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space
allowed";
}
}
if (empty($_POST["phone"])) {
$phoneErr = "Phone number is required";
}
else {
$phone = $_POST["phone"];
// check if phone only contains numbers for phone
if (!preg_match('/^\(?[\d]{3}\)?\s?\-?[\d]{3}\s?\-?[\d]
{4}$/', $phone)) {
$phoneErr = "Only numbers allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
else {
$email = $_POST["email"];
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if($nameErr or $emailErr or $phoneErr){
echo $nameErr;
echo $phoneErr;
echo $emailErr;
die();
}
else if($data = $_POST['name'] . "\n" .
$_POST['email'] . "\n" .
$_POST['phone'] . "\n" .
$_POST['county'] . "\n" .
$_POST['floor'] . "\n" .
$_POST['descr'] . "\n"){
echo "Thank you for your inquery!" . "<br/>" .
"An estimator will be with you shortly.";
mail('theshadowcallsu@gmail.com', 'Estimation Request',
$data);
}
}
?>