🌐
W3Schools
w3schools.com › php › php_form_validation.asp
PHP Form Validation
These pages will show how to process PHP forms with security in mind. Proper validation of form data is important to protect your form from hackers and spammers!
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-form-validation
PHP Form Validation - GeeksforGeeks
July 23, 2025 - <!-- file - index.php --> <!DOCTYPE html> <html> <head> <title>PHP Form Validation</title> <link rel="stylesheet" href="style.css"> </head> <body> <?php require "validation.php" ?> <p class="msg">A Simple Registration Form ?</p> <span class="error">(*) indicates required field</span> <!-- Using 'post' method for secure data sharing --> <form method="post" class="container" role="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <table> <tr class="tag"> <td><label for="userName">Enter your Username</label> <span class="error">*</span> </td> <td><input type="text" name="userN
Discussions

Form validation using PHP - Stack Overflow
I want to validate my form so ALL of the fields are required. If a field is NOT inserted or left blank it will display an error message AFTER submission. Could anyone help? Form ... More on stackoverflow.com
🌐 stackoverflow.com
PHP Form Validation

I want to know what I should look for security wise when I validate form input with php.

Depends on what the input is.

Currently I am only using regular expressions to test input, is that enough?

No. Regex only helps you test for textual patterns, and certainly isn't enough if you're doing anything beyond that.

More on reddit.com
🌐 r/PHP
18
7
March 4, 2009
Creating simple form validation and formatting elements in php?

How can i create a simple validation - just check if field isn't empty and how do i force user to type in numbers only

For simple validation I would rely on javascript/jquery/html to force your parameters

<input type="text" onKeyPress="if(this.value.length==6) return false;" id="number_id_field" required/>

if(this.value.length==6)

This only allows 6 digits in the text box, you can set it to whatever you like

This jquery function checks the key mapping on the key pressed and only allows numbers

 //Function to only allow characters 0-9, if any other key is pressed, it will be deleted immediately. Works by 
checking the key mapping of keyboard
$('#number_id_field').keyup(function (event) {
  if (event.which !== 8 && event.which !== 0 && event.which < 48 || event.which > 57) {
    $(this).val(function (index, value) {
      return value.replace(/\D/g, "");
    });
  }
});

required/

This makes the form input required so it cant be null

If you want to check that its numeric only using php, you can use a regular expression check

if (!preg_match('/^\d*$/', $number)) {
        return json_encode("This is not a number");
    }

For removing whitespace at the beginning and end of strings, I would look into using ltrtim and rtrim

They are both functions for literally trimming whitespace at the beginning and end of strings respectively

More on reddit.com
🌐 r/PHPhelp
10
1
July 29, 2019
How do you validate data?
I like https://github.com/rakit/validation which is basically a standalone clone of Laravel's validation. I'm not as much a fan of annotations/attributes for validation personally (like Symfony's), feels awkward to me. More on reddit.com
🌐 r/PHP
29
13
February 8, 2022
🌐
Mailtrap
mailtrap.io › blog › php-form-validation
PHP Form Validation: Tutorial with Code Snippets [2026]
September 11, 2025 - In this article, we’ll cover different types of validation for a simple registration form, starting with a quick overview of the available options. Ready to deliver your emails? Try Mailtrap for Free · There are a few options to validate a PHP form – you can use a PHP script, Ajax, JS, ...
🌐
Tutorialspoint
tutorialspoint.com › home › php › php form validation example
PHP Form Validation Example
May 26, 2007 - $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 below shows the form with required field validation · <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $nameErr = $emailErr = $genderErr = $websiteErr = ""; $name = $email = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST")
🌐
PHP
php.net › manual › en › filter.examples.validation.php
PHP: Validation - Manual
<?php $email_a = 'joe@example.com'; $email_b = 'bogus'; if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) { echo "Email address '$email_a' is considered valid.\n"; } if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) { echo "Email address '$email_b' is considered valid.\n"; } else { echo "Email address '$email_b' is considered invalid.\n"; } ?>
🌐
Codecademy
codecademy.com › learn › learn-php › modules › php-form-validation › cheatsheet
Learn PHP: PHP Form Validation Cheatsheet | Codecademy
For example, FILTER_VALIDATE_EMAIL returns the variable if it contains only valid email characters. Otherwise, it returns false. echo filter_var("<p>u</p>[email protected]", FILTER_SANITIZE_EMAIL); ... In PHP, htmlspecialchars is a function that transforms special characters into HTML entities.
🌐
Simplilearn
simplilearn.com › home › resources › software development › php tutorial › php form validation: an in-depth guide to form validation in php
PHP Form Validation: An In-Depth Guide to Form Validation in PHP
July 11, 2024 - Learn how to write the code for PHP form validation and various types of validation in PHP with code explanation in this in-depth tutorial. Start right away!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
SitePoint
sitepoint.com › blog › patterns & practices › form validation with php
Form Validation with PHP — SitePoint
November 13, 2024 - If one or more fields are empty, the form will be displayed again. This time, however, the empty fields will be have the error string “Missing” next to them. If none of the fields are empty, the supplied values will be displayed, in a simplistic fashion. You can find the code for this article on GitHub. Utilize PHP to validate HTML form inputs, ensuring all required fields are filled and display error messages for any missing entries.
Find elsewhere
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php form validation
A Practical Guide to PHP Form Validation By Examples
April 7, 2025 - $_POST['email']; }Code language: PHP (php) The following form requests you to enter your age and validate it as an integer with the valid range of (0,150):
🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-form-validation.php
PHP Form Validation - Tutorial Republic
In this tutorial you will learn how to sanitize and validate the user inputs submitted through a contact form using the PHP filters.
🌐
Codecademy
codecademy.com › learn › learn-php-form-handling-and-validation › modules › learn-php-form-validation › cheatsheet
Learn PHP: Form Handling and Validation: PHP Form Validation Cheatsheet | Codecademy
Learn how to handle HTML forms and validate user data before storing it in a database. ... $_POST, the PHP superglobal variable, is an array that contains data from the client’s POST request.
🌐
w3resource
w3resource.com › php › form › php-form-validation.php
PHP Form validation - w3resource
August 19, 2022 - You will see how to validate various fields used in general, like text, list, checkbox, radio button and we will also see how to retain POST data, so that after the user submits the form, even if the data supplied is not valid, data is not lost. ... Following is a live demo of the PHP form we will create by the end of this tutorial.
🌐
Tutorialspoint
tutorialspoint.com › home › php › php form validation required
PHP Form Validation Required
May 26, 2007 - URL Validation: Need to check if a valid website URL is entered by the user. Length Check: You have to limit how many characters a user can enter in the form. Pattern Matching: You can also use regular expressions to allow only specific characters.
🌐
Phppot
phppot.com › php › php-form-validation
PHP Form Validation - Phppot
PHP provides empty() function to check a variable is empty. We are using this function to check if all the text fields are empty or not. We are using isset() to check whether the gender radio button is checked or not.
🌐
HTML Form Guide
html.form.guide › php-form › php-form-validation
PHP Form Validation Script | HTML Form Guide
This generic PHP form validator script makes it very easy to add validations to your form. We create and associate a set of “validation descriptors” with each element in the form. The “validation descriptor” is a string specifying the type of validation to be performed.
🌐
PHP
pear.php.net › manual › en › package.html.html-quickform.intro-validation.php
validation and filters – How to process submitted data
In this section, we will explore the different possibilities QuickForm offers to make validation easier. QuickForm can verify if required elements are filled when the form is submitted. This works with every type of elements or groups, integer 0 is not considered as an empty value.
Top answer
1 of 7
5

I would do something like this:

$req = ['field1', 'field2', 'field...'];
$status = true;
foreach ($req as $field) {
    if (empty($_POST[$field])) {
        echo 'Field ' . $field . ' is empty';
        $status = false;
    }
}
if ($status) {
    // ok
} else {
    // not okay!
}

You create an array ($req), with all field names and loop over them. Check every field against empty() (check the php manual for this function).

Here is a better (and mostly) correct HTML snippet... Please indent properly and read any HTML tutorial for well formed code. Your HTML is **.

<?php

$value=$_POST["valuelist"];
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

$fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse");

?>
<html>
<head>
    <title>Form Input Data</title> 
</head>
<body>

    <form method="post" action="insert_ac.php"> 

    <table border="1" bgcolor="lightblue">
        <tr>
            <td align="left"><strong>Nurse Information</strong></td>
        </tr>
        <tr>
            <td><font color="red">Please select your name</font></td>
        </tr>
        <tr>
            <td>Fullname</td>
            <td>
                <select name="valuelist">
                    <option value="valuelist" value="<?php echo $nurse_name;  ?>"></option>
                    <?php

                    while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) {
                        echo '<option value="'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>';
                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Please register name here:</td>
        </tr>
        <tr>  
            <td>Fullname</td>
            <td><input type="text" name="nurse_forename" size="30"> </td>
        </tr>
    </table>
    </form>
</body>
</html>

If you have only the two given fields, this would do it:

$status = false;
$name = '';

if (!empty($_POST['nurse_forename'])) {
    $name = $_POST['nurse_forename'];
    $status = true;

} elseif (!empty($_POST['valuelist'])) {
    $name = $_POST['valuelist'];
    $status = true;

} else {

    $status = false;
    // none of nurse_forname OR valuelist is filled
    // abort.
}
2 of 7
2

Something like

foreach($_POST as $form_entry)
 if(empty($form_entry))
  echo 'you have to fill in all fields';
🌐
FormGet
formget.com › home › php › form validation using php
Form Validation Using PHP | FormGet
June 7, 2014 - We have already explain about form validation using javascript and jQuery, but this time we will show you how to validate your form using PHP. Very first we have to create a form in html setting action “#” and method “POST” with some fields, when a user clicks on submit button all the data starts travel in URL but it will be hidden, as we set method = “POST”.
🌐
GitHub
github.com › rlanvin › php-form
GitHub - rlanvin/php-form: Lightweight form validation library for PHP · GitHub
// create the form with rules $form = new Form\Validator([ 'name' => ['required', 'trim', 'max_length' => 255], 'email' => ['required', 'email'] ]); if ( $form->validate($_POST) ) { // $_POST data is valid $form->getValues(); // returns an array of sanitized values } else { // $_POST data is not valid $form->getErrors(); // contains the errors $form->getValues(); // can be used to repopulate the form } Complete doc is available in the wiki. ... If you are stuck with PHP 5.3, you may still use version 1.1.
Starred by 34 users
Forked by 7 users
Languages   PHP
🌐
HTML Form Guide
html.form.guide › php-form › php-form-validation-tutorial
PHP Form Validation Tutorial | HTML Form Guide
First, let us see how the form data appears in the server side script. The php script below will print the $_POST global array: <?php echo '<pre>'.print_r($_POST,true).'</pre>'; ?> ... To validate mandatory fields, we just have to check the presence of the value in the $_POST array.