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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
There are 2 kinds of validation available in PHP:
- Client-Side Validation: This type of validation is done on the web browser of the client machine.
- 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 [email protected].
- 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:
<blockquote> <p>Name : <input name="name" type="text"> <br><br> E-mail : <input name="email" type="text"><br><br> Website: <input name="website" type="text"><br><br> Comment: <textarea cols="40" name="comment" rows="5"></textarea></p> </blockquote>
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:<br><br> <input type="radio" name="gender" value="female">Female<br><br> <input type="radio" name="gender" value="male">Male<br><br> <input type="radio" name="gender" value="other">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:
Your Input:"; echo $name; echo ""; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>
Output:
Explanation to the above program: In the above example, we have combined all the different fields available such as text field, radio buttons and also email format types. The output will be displayed in the form of a box which will be according to the specification we give in the HTML code. Upon entering the mandatory values, the same are displayed under the text “The details entered are:”. Suppose any one of the mandatory values is not given then it will throw an error message besides that particular attribute saying that it is mandatory and needs to be filled.
Advantages of Validation in PHP
- By using client-side validation we can receive feedback faster without having to switch off from the server and download another HTML page.
- Validation on the client-side also helps in saving load on the server and increases performance.
Conclusion
We have seen in this tutorial how to do validation in PHP on the client-side and how much it is essential. It helps in saving time hence allowing for more bandwidth to point out the end-user their mistake in filling the given form. However, server-side validation is also important which is done using JavaScript, etc. Hence we can finally conclude by saying that both client and server-side validations are important since they complement each other in serving the end purpose.
The above is the detailed content of Validation in PHP. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
