PHP Form Validation

PHP form validation means to check the required input field is filled or not in proper format in the form.

How many types of Validation?
There are two types of validation
Client Side Validation (This validation performed at the client side means client browsers)
Server Side Validation (This validation performed at the server side)

Validation Rules for some input fields

Field Validation Rules

Name: Name field contains letters and white space only

Email Id: Email Id must contain @ and.

Mobile No: Mobile No contain only numeric values

Dropdown: Must be select at least once

Radio button: Must be select at least once

Check Box: Must be checked at least once

Validate the form data

The first thing we have to do that pass all variables to the validate_input function.

In this, we use three different function which performs different task

trim() function

Removes whitespace and other predefined characters from both sides of a string

stripslashes() function

Removes backslashes added. This function can be used to clean up data retrieved from an HTML form.

htmlspecialchars() function

convert the predefined characters like < (less than), > (greater than) etc. in to HTML entities

Now, we can check each $_POST variable with the validate_input() function, and the script looks like this:


// define variables and set to empty values
$name = $email = $gender = $comment = $mobile = "";
$name = validate_input($_POST["name"]);
$email = validate_input($_POST["email"]);
$mobile = validate_input($_POST["mobile"]);
$comment = validate_input($_POST["comment"]);
$sex = validate_input($_POST["sex"]);
function validate_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;

Find us on

Facebook Google Plus Twitter Pinterest YouTube

web hit counter