will be helpful for those in need Further to my comment, here is what I meant by passing the variables as an array then looping over them, applying your patterns, and passing $errors by reference:
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.<br>",
"lname" => "Please enter your LAST name between 2 and 15 letters.<br>",
"phone" => "Please enter your phone number in (###)###-### format.<br>",
"city" => "Please enter your city name between 3 and 20 letters.<br>",
"state" => "Please enter your state's abbreviation (Example: CA).<br>",
);
// Pass by ref--------------------------------vv
function validateFormInput($input, $patterns, &$errors)
{
$new = false;
foreach($input as $key => $value) {
if(preg_match($patterns[$key], $value, $match))
unset($errors[$key]);
}
return (empty($errors));
}
$valid = false;
if(isset($_POST['submit'])) {
$inputs = $_POST;
unset($inputs['submit']);
$valid = validateFormInput($inputs,$patterns,$errors);
if(!empty($errors))
echo implode("<br />",$errors);
}
if(!$valid) {
?>
<form method="post">
<input type="text" name="fname" value="Name" />
<input type="text" name="lname" value="LastName" />
<input type="text" name="phone" value="123-123-1233" />
<input type="text" name="city" value="Reno" />
<input type="text" name="state" value="NsrV" />
<input type="submit" name="submit" value="submit" />
</form>
<?php }
define("ERRMODE",true);
function validateFormInput($input)
{
$errors = false;
foreach($input as $key => $value) {
$pattern = patternList($key);
if(!$pattern)
continue;
if(!preg_match($pattern, trim($value), $match)) {
$errors[$key] = errorMsg($key);
}
}
return $errors;
}
function patternList($name = false)
{
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
return (isset($patterns[$name]))? $patterns[$name]:false;
}
function errorMsg($name = false)
{
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.",
"lname" => "Please enter your LAST name between 2 and 15 letters.",
"phone" => "Please enter your phone number in (###)###-### format.",
"city" => "Please enter your city name between 3 and 20 letters.",
"state" => "Please enter your state's abbreviation (Example: CA).",
);
return (isset($errors[$name]))? $errors[$name]:false;
}
function printPre($val = false)
{
ob_start();
echo "<pre>";
print_r($_POST);
echo "</pre>";
$data = ob_get_contents();
ob_end_clean();
return $data;
}
if(isset($_POST['submit'])) {
if(ERRMODE) {
echo printPre($_POST);
echo printPre($_FILES);
}
$errors = validateFormInput($_POST);
$formValid = (empty($errors));
$fname = htmlspecialchars($_POST['fname']);
$lname = htmlspecialchars($_POST['lname']);
$phone = htmlspecialchars($_POST['phone']);
$city = htmlspecialchars($_POST['city']);
$state = htmlspecialchars($_POST['state']);
$mobile = preg_replace('/\D+/', '', $phone);
}
?><!DOCTYPE html>
<html>
<head>
<title>Lesson 7</title>
<meta charset="UTF-8">
<meta name="description" content="php">
<meta name="keywords" content="php">
<meta name="author" content="William Payne">
<link rel="stylesheet" type="text/css" href="xxxxxxxxxxxx">
</head>
<body>
<div class="formLayout">
<?php
//form
if (empty($formValid)) {
if(!empty($errors))
echo implode("<br />",$errors);
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Info</legend>
<?php echo $error_text ?>
<label>First Name:</label><input name="fname" type="text" value="<?php echo (!empty($fname))? $fname : ""; ?>"><br>
<label>Last Name:</label><input name="lname" type="text" value="<?php echo (!empty($lname))? $lname : ""; ?>"><br>
<label>Phone Number:</label><input name="phone" type="text" value="<?php echo (!empty($phone))? $phone : ""; ?>"><br>
<label>City:</label><input name="city" type="text" value="<?php echo (!empty($city))? $city : ""; ?>"><br>
<label>State:</label><input name="state" type="text" value="<?php echo (!empty($state))? $state : ""; ?>"><br>
</fieldset>
<input name="submit" id="submit" type="submit" value="submit"><br>
</form>
<?php
} else {
?>
<p><strong><?php echo "$welcome_msg"; ?></strong></p>
<p><strong><?php echo "{$lname}, {$fname}";?></strong></p>
<p><strong><?php echo "{$phone}"; ?></strong></p>
<p><strong><?php echo "{$city}, {$state}"; ?></strong></p>
<?php
}
?>
</div>
</body>
</html>