ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Server Side Form Validation Using Regular Expressions in PHP

RegularExpresionPHPIn this tutorial, I will teach on how to validate a form using regular expressions in 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;
}

/*centered form*/
@media(min-width:768px) {
 .content {
 position: inherit;
 margin: 0 400px;
 }

 .row > .input {
 width: 100%;
 margin: 0;
 }

 .row > .button {
 width: auto;
 margin-left: 85px;
 }
}

/*default width of the container*/
#container {
 width: 100%;
}

/*for the content*/
#container > .content {
 background: #dedede;
 position: inherit;
 padding: 10px;
}

/*for the input*/
.row > .input {
 width: 80%;
 display: inline-block;
 height: 30px;
}

/*for the label*/
.row > .label {
 width: 80px;
 display: inline-block;
}

/*for the row*/
.content > .row {
 padding: 0 0 5px;
}

/*for the error message*/
.row > #errormsg {
 color: red;
 position: absolute;
 display: inline-block;
 font-size: 12px;
 width: auto;
 padding: 2px;
}

/*for the button*/
.row > .button {
 height: 30px;
 position: inherit;
}
</style>
  • Create a page for the validating process.
<?php
/*it will Execute when the form was submitted*/
if(isset($_POST['submit'])){

    /*pass the content of the input box to the specific variables*/
    $name = $_POST['name'];
    $email = $_POST['email'];
    $contact = $_POST['contact'];
    $address = $_POST['address'];

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

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

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

    }
 /*validating contact number*/

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

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

    /*for successfull validation*/
    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 – jannopalacios@gmail.com
Mobile No. – 09305235027 – tnt

 

Download Sourcecode

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!