₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,327,331 members, 8,430,433 topics. Date: Saturday, 20 June 2026 at 01:13 PM

Toggle theme

How To Validate A Form In PHP - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingHow To Validate A Form In PHP (356 Views)

1 Reply

How To Validate A Form In PHP by Zhully(op): 1:51pm On Nov 08, 2020
Hello nairalanders, I know some of you think PHP is old and outdated, but to be sincere, PHP powers most websites in the world. It will be a great opportunity for you to learn this programming language.

I will be posting more on PHP and will be glad if you can visit my blog for more. https://zhullyblogg..com/p/php-tutorial-zhullyblog.html?m=1
Now, the tutorial.

What is validation?
When we talk about validation, we are talking about checking whether the user enters an input correctly. Let's take a form for instance. When you create a form, you need user to enter either their name correctly, enter their email correctly, - and the only way to do this is by validating the form. So if the user fails to enter input correctly, the form is redisplayed with an error message.

There are several inputs that you need to validate in a form such as name , Email address etc.
Let's break it down



PHP - Validate name
A name will contain letters and whitespace. The name could be uppercase or lowercase letters. So, if the user fail to enter correctly, it will display an error message.
Here is the code.
statement.




$name =
test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z]*$/",$name)) {
$nameErr ="Only letters are allowed";
}



Note: The preg_match( ) function will return true if the input meets the condition and false if it fails to meet the condition.



PHP Validate Email
To validate email ( to check whether the input matches email pattern) , you use the filter_var( ) function. So if the email entered does not meet the requirements, an error message will be displayed.



$email =
test_input($_POST["email"]);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid Email";
}

Read more on validation.
https://zhullyblogg..com/2020/06/php-form-validation-how-to-validate.html?m=1
1 Reply

Python With MYSQL: Registration Form In Python Using Tkinter And MYSQL DatabaseHow Do I Add Paystack Form In My WebsiteHow To Add A Contact Form In Magento 2 Sidebar Widget234

Please Help:i Need A Expert ProgrammerUse Multi-select Autocomplete Jquery With Example.I Need Of An App Builder.