Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Php Form Validation Example

Php Form Validation Example

Posted on June 10, 2018December 17, 2022 By XpertPhp 27 Comments on Php Form Validation Example

Today, We will explain to you how to make form validation in PHP(Php Form Validation Example). when the user submitted the form at that time required for input field validation. there are two types of validation, the first is server-side and the second is client-side. see below example of server-side validation(Php Form Validation Example).

php_form_validation

Required field, email validation and number required .. etc I will give validation information by our post.

Read Also:
Jquery Form Validation Example

See below code, that is an array wise creating a form validation, whenever error generate at that time count error array element, if count greater than 0, we are showing the error message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
if(isset($_POST['btnadd']))
{
    $first_name  = $_POST['txtFname'];
    $last_name   = $_POST['txtLname'];
    $address     = $_POST['txtAddress'];
    $email       = $_POST['txtEmail'];
    $mobile      = $_POST['txtMobile'];
$error=array();
if(empty($first_name)){
   $error['first_name']="First Name is required";
}
if(empty($last_name)){
   $error['last_name']= "Last Name is required";
}
if(empty($email)){
   $error['email']= "Email is required";
}
else
{
   if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error['email']= "Invalid email format";
   }
}
if (empty($mobile)) {
   $error['mobile']= "Mobile is required";
}
else{
   if (preg_match('/[^0-9]+$/', $mobile)){
$error['mobile']= "Only Number allowed";
   }
}
$count=count($error);
if($count>0){
echo "Validation is Success";
}
}
?>
<style>
     .error {color: #FF0000;}
</style>
<form method="post" name="frmAdd">
        <table align="center">
            <tr>
                <td colspan="2" align="center">Add Record</td>
            </tr>
 
            <tr>
                <td>First Name</td>
                <td>
<input type="text" name="txtFname" value="<?php echo @$first_name; ?>">
<span class = "error">* <?php echo '<br>'.@$error['first_name'];?></span>
</td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td>
<input type="text" name="txtLname" value="<?php echo @$last_name; ?>">
<span class = "error">* <?php echo '<br>'.@$error['last_name'];?></span>
               </td>
            </tr>
            <tr>
                <td>Address</td>
                <td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td>
            </tr>
            <tr>
                <td>Email</td>
                <td>
<input type="text" name="txtEmail" value="<?php echo @$email; ?>">
<span class = "error">* <?php echo '<br>'.@$error['email'];?></span>
</td>
            </tr>
            <tr>
                <td>Mobile</td>
                <td>
  <input type="text" name="txtMobile" value="<?php echo @$mobile; ?>">
  <span class = "error">* <?php echo '<br>'.@$error['mobile'];?></span>
</td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                  <input type="submit" value="Add" name="btnadd">
               </td>
            </tr>
        </table>
</form>
See also  How to Remove file Extension from Filename in PHP

Php Tags:form validation, how to create form in php, html php form, php validate email, registration form validation using php, server side validation in php, validation

Post navigation

Previous Post: How to Create Multi level Nested category in Codeigniter
Next Post: Jquery Form Validation Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com