How many types of validation are there in php?

Introduction to Validation in PHP

Validation in PHP is the process where we check if the input information in the various fields in any form such as text, checkbox or radio button, etc, submitted by the end-user in the form is correct or not using HTML code.

There are 2 kinds of validation available in PHP:

  1. Client-Side Validation: This type of validation is done on the web browser of the client machine.
  2. Server-Side Validation: The data on submission is then sent to the server machine to perform validation checks there.

Rules to Validation in PHP

There are certain rules for validation of a form and they are as follows for the following fields:

  • Name: It is a mandatory field whose data contains only whitespace and letters.
  • Email: It is also a mandatory field that should contain a valid email address means it should be in the format of .
  • Website: It is an optional field and should contain a valid URL if given
  • Comment: It is an optional field and is a multi-line text input where we can give long sentences.
  • Gender: It is a mandatory field and should select one gender out of the female, male and other.

Types of Validation in PHP

Now let us see how to validate different types of such fields.

1. Validation of text fields

There are a few text fields from the above-said attributes such as name, email, website, and comment. The HTML code for the same is as below:

Code:

Name :

E-mail :

Website:

Comment:

Output:

Explanation to the above program: Here we can customize these attribute names however we want by using different header tags and also space tags. We have shown one of the paragraph format options for this one here.

2. Validation of radio buttons

Radio buttons are basically the ones having boolean value either true or false and they are represented by a circular box kind of a thing. If the box is circled it means true and vice versa. Let us check out the HTML code to implement the same for above said “Gender” attribute.

Code:

Gender:

Female

Male

Other

Output:

Explanation to the above program: By using this code we are displaying 3 options under the gender category as male, female and other. The user can circle on the required option by clicking on the circle and the option will be saved and validated.

3. Validation of form element

This method is used at the end after all the required details have been filled to submit the form. On using this HTML code the form data when submitted is sent with the “post” method. There are basically 2 attributes we need to specify while validating form element: action and method. We can convert certain HTML attributes to their respective entity names in order to prevent the form breaking when the user submits. “action” determines where the data of the form is sent on submission and “method” defines how the form’s data is submitted.

Code:

Chủ Đề