What is validation in php



Required field will check whether the field is filled or not in the proper way. Most of cases we will use the * symbol for required field.

What is Validation ?

Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows −

  • Client-Side Validation − Validation is performed on the client machine web browsers.

  • Server Side Validation − After submitted by data, The data has sent to a server and perform validation checks in server machine.

Some of Validation rules for field

FieldValidation Rules
Name Should required letters and white-spaces
Email Should required @ and .
Website Should required a valid URL
Radio Must be selectable at least once
Check Box Must be checkable at least once
Drop Down menu Must be selectable at least once

Valid URL

Below code shows validation of URL

$website = input($_POST["site"]);

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
   $websiteErr = "Invalid URL"; 
}

Above syntax will verify whether a given URL is valid or not. It should allow some keywords as https, ftp, www, a-z, 0-9,..etc..

Valid Email

Below code shows validation of Email address

$email = input($_POST["email"]);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $emailErr = "Invalid format and please re-enter valid email"; 
}

Above syntax will verify whether given Email address is well-formed or not.if it is not, it will show an error message.

Example

Example below shows the form with required field validation


   
   
      
   
   
   
      
     
      

Absolute classes registration

* required field.

">
Name: *
E-mail: *
Time:
Classes:
Gender: Female Male *
Your given values are as:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>

It will produce the following result −

What is validation in php

Can validation be done in PHP?

PHP validates the data at the server-side, which is submitted by HTML form. You need to validate a few things: Empty String. Validate String.

What is the use of validation?

The most basic use of validation functions is to ensure that documents are properly formed to fit your application's expectations. Without validation, you need to check for the existence of all fields on a document that your MapReduce or user-interface code needs to function.

What is the importance of validation in PHP?

Importance of PHP Form Validation Validates data as float, & translates to float on success. Validates data as integer, & translates to integer on success.

What are the two types of validation?

There are 4 main types of validation:.
Prospective Validation..
Concurrent Validation..
Retrospective Validation..
Revalidation (Periodic and After Change).