Server Side Form Validation Using Regular Expressions Using PHP

RegularExpresionPHP

Validation Form Using PHP

In this tutorial, I will teach on how to create a Validation Form Using PHP. It focus on the server side validation of the registration form. This is the easiest way to validate the fields in the form to its correct format. it is also a good practice that can help you improve your skills in developing web applications.

Below are the step by step process for validating a form using regular expressions in PHP.

  • Create a style for your design:
<style type="text/css">
body {
 background: #eee;
}


    if($name==''){
        $errorName = "Contact is required!";
    }else{

        if(preg_match('/^[A-z\s]*$/',$name)){
           $name;
        }else{
         $errorName = "Name  is not valid!";
        }
    }

 
    if($email==''){
        $errorEmail = "Email is required!";
    }else{
        if(filter_var($email, FILTER_VALIDATE_EMAIL)){
         $email;
        }else{
         $errorEmail = "Email  is not valid!";
        }

    }
 

    if($contact==''){
        $errorContact = "Contact is required!";
    }else{
        if(preg_match('/^[0-9_-]*$/', $contact)){
            $contact;
        }else{
            $errorContact = "Contact number  is invalid!";
        }

    }
        
     
    if($address==''){
         $errorAddress = "Address is required!";
    }else{
        if(preg_match('/^[a-zA-Z-0-9_-\s]*$/', $address)){
           $address;
        }else{
         $errorAddress = "Address is not valid!";
        }
 
    }

    
    if(!isset($errorAddress) && !isset($errorName) && !isset($errorEmail)  && !isset($errorContact)){
        echo "<h1 align='center'>You are now successfully registered</h1>";
        echo "<style> .content { display:none; }</style>";
    }
}
?>
  • Create a landing page and name it “index.php”.
 <!DOCTYPE html>
<html>
<head>
 <title>Server Side Validation</title>
</head>
<body>
 <form action="" id="container" method="post" name="container">
 <div class="content">
 <h1>Register Now!</h1>
 <div class="row">
 <label class="label">Name:</label> 
 <input class="input" name="name" type="text" value="<?php echo !isset($errorName)? $name : ''?>"> 
 <!-- validate the error message -->
 <?php echo isset($errorName) ? '<span id="errormsg">'.$errorName.'</span>' : ''?>
 </div>
 <div class="row">
 <label class="label">Email:</label> 
 <input class="input" name="email" type="text" value="<?php echo !isset($errorEmail)? $email : ''?>"> 
 <!-- validate the error message -->
 <?php echo isset($errorEmail) ? '<span id="errormsg">'.$errorEmail.'</span>' : ''?>
 </div>
 <div class="row">
 <label class="label">Contact #:</label> 
 <input class="input" name="contact" type="text" value="<?php echo !isset($errorContact)? $contact : ''?>"> 
 <!-- validate the error message -->
 <?php echo isset($errorContact) ? '<span id="errormsg">'.$errorContact.'</span>' : ''?>
 </div>
 <div class="row">
 <label class="label">Address:</label> 
 <input class="input" name="address" type="text" value="<?php echo !isset($errorAddress)? $address : ''?>"> 
 <!-- validate the error message -->
 <?php echo isset($errorAddress) ? '<span id="errormsg">'.$errorAddress.'</span>' : ''?>
 </div>
 <div class="row">
 <button class="button" name="submit" type="submit">Submit</button>
 </div>
 </div>
 </form>
</body>
</html>

For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – tnt

Download Sourcecode

Leave a Comment