Today we will learn about validation of the form elements in this PHP Form Validation tutorial.
While entering input in the form, user can enter wrong input may be by mistake or purposefully. This will create problem for processing the form. The programmer should not rely on user or should not have full faith in user that he/she will enter correct data only. To avoid any wrong input data it is programmer’s responsibility to stop the user from putting any wrong input. This can be done using validations for the form input elements. This will not allow the user to put letters where numbers are required and vice-a-versa.
- What is Validation?
- Validation is a task used to check the input entered by user in a form, to avoid errors in the output.
- When the user clicks on submit button after filling the form, the input is first validated and then it is processed further.
- If the input entered by user is not correct, the user is again asked to enter the correct information by giving error messages instead of processing it.
- Types of Validation:
- There are 2 types of validations:
- Client-Side Validation:
- When the validation is performed on client machine, it is called as client-side validation.
- Here all information is not sent to server for validation, instead it is validated at client-side itself.
- This is a fast method of validation and at the same time it minimizes the unnecessary load on server.
- Server-side Validation:
- When the validation is performed on server, it is called as server-side validation.
- The data is sent to the server for validation and if any error occurs in validation, it is sent to the client to correct the input.
- Validation done at server-side is reliable and secure.
- Validation functions:
- There are 2 validation functions:
- Basic Validation:
- The form is checked against every form field, whether the input is entered in every field or not.
- Data Format Validation:
- It checks if the form element contains the correct data.
- It checks whether the data is in correct format such as of email-id etc.
- It checks the field requiring numbers has numbers only; the field requiring string has string only, etc.
- Validation:
- Let us demonstrate the validation with the help of an example and understand the code.
- There are 2 methods to validate the page:
- Validate the input by checking it in the same page at client-side by using $_SERVER[“PHP-SELF”] in the action attribute of the form.
- Validate the input by sending it at the server-side by putting the desired page URL in the action attribute of the form.
- To demonstrate it, let us create a new folder named Validation in the htdocs folder in xampp folder in C drive. Then open a new notepad++ document and save it as index.php in the newly created Validation folder.
- The code for a simple HTML form is given below:
<html> <head> <title>PHP Form Validation</title> <style> h1{ margin-left:19px; } label{ margin-left:10px; display:inline-block; width:120px; } #decor{ background:yellow; color:brown; font-weight:bold; } .shift{ padding:5px; margin-left:120px; color:brown; font-weight:bold; } </style> </head> <body id="decor"> <h1>Customer Details</h1> <form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label for="name">Full Name:</label> <input type="text" name="name"/>*<br> <label for="addr">Address:</label> <textarea name="addr" rows="4" cols="17"></textarea><br> <label for="city">City:</label> <input type="text" name="city"/>*<br> <label for="state">State:</label> <input type="text" name="state"/>*<br> <label for="mob">Mobile Number:</label> <input type="text" name="mob"/>*<br> <input class="shift" type="submit" value="SAVE"/> </form> </body> </html>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php $errormsg=""; $valid=false; if($_SERVER["REQUEST_METHOD"]=="POST") { $name=$_POST["name"]; $addr=$_POST["addr"]; $city=$_POST["city"]; $state=$_POST["state"]; $mob=$_POST["mob"]; if(empty($name)) { $errormsg.='<p class="error">Name cannot be blank.</p>'; $valid=false; } else { if(is_string($name)) $valid=true; else { $errormsg.='<p class="error">Name should be in string format.</p>'; $valid=false; } } if(empty($city)) { $errormsg.='<p class="error">City is compulsory.</p>'; $valid=false; } else { if(is_string($city)) $valid=true; else { $errormsg.='<p class="error">City should be in string format.</p>'; $valid=false; } } if(empty($state)) { $errormsg.='<p class="error">State is compulsory.</p>'; $valid=false; } else { if(is_string($state)) $valid=true; else { $errormsg.='<p class="error">State should be in string format.</p>'; $valid=false; } } if(empty($mob)) { $errormsg.='<p class="error">Mobile number is compulsory.</p>'; $valid=false; } else { $len=strlen($mob); if(is_numeric($mob) && $len==10) $valid=true; else { $errormsg.='<p class="error">Mobile number should be in number format with 10 digits in it.</p>'; $valid=false; } } if(errormsg!="") echo $errormsg; if($valid==true) header('Location:success.php'); } ?> <html> <head> <title>PHP Form Validation</title> <style> h1{ margin-left:19px; } label{ margin-left:10px; display:inline-block; width:120px; } #decor{ background:yellow; color:brown; font-weight:bold; } .shift{ padding:5px; margin-left:120px; color:brown; font-weight:bold; } .error{ color:red; } </style> </head> <body id="decor"> <h1>Customer Details</h1> <form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> * required fields<br> <label for="name">Full Name:</label> <input type="text" name="name"/>*<br> <label for="addr">Address:</label> <textarea name="addr" rows="4" cols="17"></textarea><br> <label for="city">City:</label> <input type="text" name="city"/>*<br> <label for="state">State:</label> <input type="text" name="state"/>*<br> <label for="mob">Mobile Number:</label> <input type="text" name="mob"/>*<br> <input class="shift" type="submit" value="SAVE"/> </form> </body> </html>
<?php $errormsg=""; $valid=false; if($_SERVER["REQUEST_METHOD"]=="POST") { $name=$_POST["name"]; $addr=$_POST["addr"]; $city=$_POST["city"]; $state=$_POST["state"]; $mob=$_POST["mob"]; if(empty($name)) { $errormsg.='<p class="error">* Name cannot be blank.</p>'; $valid=false; } else { if(is_string($name)) $valid=true; else { $errormsg.='<p class="error">* Name should be in string format.</p>'; $valid=false; } } if(empty($city)) { $errormsg.='<p class="error">* City is compulsory.</p>'; $valid=false; } else { if(is_string($city)) $valid=true; else { $errormsg.='<p class="error">* City should be in string format.</p>'; $valid=false; } } if(empty($state)) { $errormsg.='<p class="error">* State is compulsory.</p>'; $valid=false; } else { if(is_string($state)) $valid=true; else { $errormsg.='<p class="error">* State should be in string format.</p>'; $valid=false; } } if(empty($mob)) { $errormsg.='<p class="error">* Mobile number is compulsory.</p>'; $valid=false; } else { $len=strlen($mob); if(is_numeric($mob) && $len==10) $valid=true; else { $errormsg.='<p class="error">* Mobile number should be in number format with 10 digits in it.</p>'; $valid=false; } } if(errormsg!="") echo $errormsg; if($valid==true) header('Location:success.php'); } ?>
if($_SERVER["REQUEST_METHOD"]=="POST")
Name –> $_POST[“name”];
Address –> $_POST[“addr”];
City –> $_POST[“city”];
State –> $_POST[“state”];
Mobile –> $_POST[“mob”];
$name=$_POST["name"]; $addr=$_POST["addr"]; $city=$_POST["city"]; $state=$_POST["state"]; $mob=$_POST["mob"];
if(empty($name)) { $errormsg.='<p class="error">* Name cannot be blank.</p>'; $valid=false; } else { if(is_string($name)) $valid=true; else { $errormsg.='<p class="error">* Name should be in string format.</p>'; $valid=false; } }
$errormsg = $errormsg . Another error message;
$errormsg.='<p class="error">Name cannot be blank.</p>';
if(is_string($name))
header('Location:success.php');
<body bgcolor="yellow"> <h1><font color="brown">Your record has been saved!</font></h1> </body>
Thus we studied how to check an input in the input field for its validity and how to show errors. We will see validation of some more input fields in the next session.
I got what you intend, appreciate it for posting. Woh I am lucky to find this website through google.