I have a section of code where I am wanting an error to occur when a user enters symbols and not letters into the name and lastname of the form fields. The email is working correctly when a user enters an invalid email address but it won't currently display an error if the user enters symbols in the name fields. I have attached code below, any help appreciated. Thank you
<?php
// define variables and set to empty values
$nameErr = $lastErr = $emailErr = "";
$name = $last = $email = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "First Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
echo $nameErr;
if (empty($_POST["last"])) {
$lastErr = "Last Name is required";
} else {
$last = test_input($_POST["last"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last)) {
$lastErr = "Only letters and white space allowed";
}
}
echo $lastErr;
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
echo $emailErr;
}
?>
<div class="article"><div class="box-sizing">
<h1>Alert Registration</h1><div class="" id=""><ul>
<p>Register to recieve alerts about shark catches in your chosen area</p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<section class="user-details">
<label for="name">First Name:</label><br>
<input name="name" type="text" size="35" value="">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<label for="last">Last Name:</label><br>
<input name="last" type="text" size="35" value="">
<span class="error">* <?php echo $lastErr;?></span>
<br><br>
<label for="email">Email Address:</label><br>
<input name="email" type="email" size="35" value="">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<label for="phone_number">Phone Number:</label><br>
<input name="phone_number" type="tel" size="35" value="">
<br><br>
<label for="password">Password</label><br>
<input name="password" type="password" size="36" value="">
<br><br>
</section>
</div>