Validate The Input Text From an HTML Form Using PHP

PHP is a powerful programming language, therefore, PHP provides a complete range of functions. An example is the function that will be used to perform validation, which will be discussed in this article. What is the importance of validating the input received from the user? Validation is very important to check if the input received is valid or not, before the input will be processed by the application. Perform validation will prevent the application provides invalid information and prevent errors during the process and can prevent SQL Injection.

isset()
This function is used to check whether a variable is set or not. Consider the following example:
if(isset($_POST['submit'])){
  echo "Variable has been set";
}
else{
  echo "Variable not set";
}
In this example, check whether the variable $_ POST ['submit'] is set or not.

empty()
The function empty() is used to check whether a variable has a value or not (null / not null). Consider the following example:
$name="John";
if(empty($name)){
  echo "Variable $name is empty!";
}
else{
  echo $name;
}
In the above example the function empty() is used to check whether the variable $name has a value or not. Previous variable $name has been set to the value "John" ($name="John") then when the script is executed will display the value of variable $name

preg_match()
preg_match often used to validate where its use is combined with Regular Expression. The following is the syntax of preg_match():
preg_match($pattern,$subject);
There are two parameters: $pattern and $subject. $pattern is the pattern to search for, as a string; $subject is the input string which will be matched by the pattern of whether it is appropriate or no.
The first example of the use preg_match, making text input validation with pattern allows the use of uppercase, lowercase, spaces and the dot.
if(!preg_match("/^[a-zA-Z.\s]+$/",$_POST['txt'])){
    echo "Please fill the form using uppercase, lowercase, spaces or the dot!";
 }
 else{
    echo "Your entry is match!";
 }
In the example script above, regular expression pattern that is used is "/^[a-zA-Z.\s]+$/", then be matched with the input from the user ($_POST ['txt']).  If the input pattern in accordance with the regular expression it will display the text "Your entry is match", if it is not appropriate to be displayed text "Please fill the form using uppercase, lowercase, spaces or the dot!".

Validation Number 
Validation numbers only numbers are allowed, for the pattern can use "/^[0-9]+$/". The following example script validation number:
if(!preg_match("/^[0-9]+$/",$_POST['txt'])){
      echo "Please fill the form using number only!";
 }
 else{
      echo "Your entry is match!";
 }
Validation Email
Regular Expression to validate the email is "/^[a-zA-Z0-9._]+@+[a-zA-Z0-9]+[._]+[a-zA-Z0-9.]+$/".
Following the implementation of the email validation script:
if(!preg_match("/^[a-zA-Z0-9._]+@+[a-zA-Z0-9]+[._]+[a-zA-Z0-9.]+$/",$_POST['txt'])){
      echo "your entry is not appropriate as the email format!";
 }
 else{
      echo "Your entry is match!";
 }
Download Script