Server Side Form Validation (Registration Form)

Validating a form using a JavaScript gives convenience to your visitors by avoiding page reload and other features that we discuss on our previous tutorial called Validate Login Page Using JavaScript. But validating a form using JavaScript alone is not safe. One reason is if the JavaScript is not enabled in the web browser. This will bypass all the validation that you have defined in your code. So, to avoid this kind of problem, all you need to do is validate it on server side. The following are the example of a registration form with validation using PHP. validate_registration.php
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Registration Form</title>
  6. </head>
  7.  
  8. <body>
  9. <?php include("validation.php"); ?>
  10. <form method="post" action="" name="form">
  11.         <table width="510" border="0">
  12.                 <tr>
  13.                         <td>&nbsp;</td>
  14.                         <td>Registration Form</td>
  15.                         <td>&nbsp;</td>
  16.                 </tr>
  17.                 <tr>
  18.                         <td>Username :</td>
  19.                         <td><input type="text" name="username" value="<?php echo $val_username; ?>" /></td>
  20.                         <td><?php echo $err_username; ?></td>
  21.                 </tr>
  22.                 <tr>
  23.                         <td>Password :</td>
  24.                         <td><input type="password" name="password" value="<?php echo $val_password; ?>" /></td>
  25.                         <td><?php echo $err_password; ?></td>
  26.                 </tr>
  27.                 <tr>
  28.                         <td>Name :</td>
  29.                         <td><input type="text" name="name" value="<?php echo $val_name; ?>" /></td>
  30.                         <td><?php echo $err_name; ?></td>
  31.                 </tr>
  32.                 <tr>
  33.                         <td>Email : </td>
  34.                         <td><input type="text" name="email" value="<?php echo $val_email; ?>" /></td>
  35.                         <td><?php echo $err_email; ?></td>
  36.                 </tr>
  37.                 <tr>
  38.                         <td>&nbsp;</td>
  39.                         <td><input type="submit" name="button" id="button" value="Submit" /></td>
  40.                         <td>&nbsp;</td>
  41.                 </tr>
  42.         </table>
  43. </form>
  44. </body>
  45. </html>
Now our validation.php script
  1. <?php
  2. if($_POST)
  3. {
  4.         $username = $_POST['username'];
  5.         $password = $_POST['password'];
  6.         $name = $_POST['name'];
  7.         $email = $_POST['email'];
  8.         // Full Name
  9.         if (!preg_match("%[^A-Za-z\s0-9 - @ .]/%",$name)){
  10.                 $val_name = $name;
  11.         }else{
  12.                 $err_name='Please enter valid Name.';
  13.         }
  14.  
  15.         // Email
  16.         if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email)) {
  17.                 $val_email = $email;
  18.         }else{
  19.                 $err_email = 'Please enter valid Email.';
  20.         }
  21.  
  22.         // Usename min 2 char max 20 char
  23.         if (preg_match('/^[a-z\d_]{2,20}$/i', $username)){
  24.                 $val_username = $username;
  25.         }else{
  26.                 $err_username = 'Please enter valid Username (minimum 3 characters)';
  27.         }
  28.  
  29.         // Password min 6 char max 20 char
  30.         if (preg_match("/^[a-z0-9_-]{6,20}$/i", $password)){
  31.                 $val_password = $password;
  32.         }else{
  33.                 $err_password = 'Please enter valid Password (minimum 6 characters)';
  34.         }
  35.  
  36.         if((strlen($val_name)>0)&&(strlen($val_email)>0)
  37.                         &&(strlen($val_username)>0)&&(strlen($val_password)>0) ){
  38.                 header("Location: home.html");
  39.         }else{ }
  40. }
  41. ?>

Add new comment