Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,975 members, 7,825,040 topics. Date: Sunday, 12 May 2024 at 01:59 AM

How To Validate A Form In PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Validate A Form In PHP (280 Views)

Python With MYSQL: Registration Form In Python Using Tkinter And MYSQL Database / How Do I Add Paystack Form In My Website / User Defined Function To Validate Regular Expression In Javascript (2) (3) (4)

(1) (Reply)

How To Validate A Form In PHP by Zhully: 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)

Become A Certified Website Developer Without Coding And No Prior Experience / Need Urgent Help With An Assignment On Basic Programming!!! / Get Best Drupal Support And Maintenance Services - Drupal India

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.