Tutorials

Welcome to the Absolute Web Dev tutorials page. All tutorials are made by request. If you need a tutorial, please email me with your request and I will create a tutorial that will fulfill your needs.

Validating a form using a PHP filter and a Regular Expression.

Written by WebMaster   |  01/11/2009

Validation in the Submittal

Once you have you validation code created, you can place them into the Conditional statement you created for when the form is submitted. (See code below) When the form is submitted, the code will execute, checking the form inputs to the conditions in the code. Whatever the outcome, the appropriate message will display.

<?php
if (isset($_POST['submitted'])){
  // Validation with PHP regular expression
  if (empty($_POST['name'])){
    echo 'No name was entered!<br />';
  }elseif (!empty($_POST['name'])){
    $name = $_POST['name'];
    $pattern = '/^[a-zA-Z]{1,25}$/';		
    if (preg_match($pattern,$name)){ 
      echo '<b>'.$name.'</b> is a valid name.<br />';
    }else{ 
      echo '<b>'.$name.'</b> is not a valid name.<br />';
    }
}
	
  // Validation with PHP Filter
  if (empty($_POST['email'])){
    echo 'No email was entered!<br />';
  }elseif (!empty($_POST['email'])){
    $email=$_POST['email'];
    if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)){
      echo '<b>'.$email.'</b> is not a valid Email address.';
    }else{
      echo '<b>'.$email.'</b> is a valid Email address';
    }
  }
}
?>

The whole code

Below is all of the code inside the appropriate HTML. You can copy and past this whole section into a PHP file and it will work for you. This concludes the PHP filter and Regular expression validation tutorial. For more information on PHP filters you can go to w3schools PHP filter references.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PHP Form Validation</title>
</head>

<body>
<h2>PHP Form Validation</h2>
<?php
if (isset($_POST['submitted'])){
  // Validation with PHP regular expression
  if (empty($_POST['name'])){
    echo 'No name was entered!<br />';
  }elseif (!empty($_POST['name'])){
    $name = $_POST['name'];
    $pattern = '/^[a-zA-Z]{1,25}$/';		
    if (preg_match($pattern,$name)){ 
      echo '<b>'.$name.'</b> is a valid name.<br />';
    }else{ 
      echo '<b>'.$name.'</b> is not a valid name.<br />';
    }
  }

  // Validation with PHP Filter
  if (empty($_POST['email'])){
    echo 'No email was entered!<br />';
  }elseif (!empty($_POST['email'])){
    $email=$_POST['email'];
    if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)){
      echo '<b>'.$email.'</b> is not a valid Email address.';
  }else{
    echo '<b>'.$email.'</b> is a valid Email address';
  }
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
  <p>Name: <input name="name" type="text" /></p>
  <p>Email: <input name="email" type="text" /></p>
  <input name="submit" type="submit" value="Validate" />
  <input name="reset" type="reset" value="Reset" />
  <input name="submitted" type="hidden" value="submitted" />
</form>
<p><a href="validate.php">Clear Errors</a></p>
</body>
</html>

Previous 1 2
Archive
TOP

  100% compatible with:
Firefox Explorer Opera Safari Flock Netscape Chrome

 100% valid CSS 3.0 and XHTML 1.0.
Valid CSS Valid XHTML