PHP Form Validation | Coding Tag

PHP Form Validation

Last Updated by Yogesh Khanna 06-Mar-20

2 5187


The PHP form validation field is very useful for all Web Pages. Whenever, any user entered value in a webpage like Registration, Registered employee id, change password, Order books, etc. Many times, some text box remains invalid format, then form validation field display an error message on a webpage like Please enter valid field.

In the below example, we used a text box for employee name, employee email id, text area to display an address of employee and the radio button to select employee language.

The employee enters an incorrect value in a text box or text area, then validation field error message display on a web page, means in a name we don't allow numeric value or any alphanumeric value then error message display, please enter the alphabets and white space.

In the email id validation field, we used FILTER_VALIDATE_EMAIL correct format for a valid email id

Step 1: PHP Form Validation Code

<?php

// variables are defined...

$employee_name=" ";
$employee_name_display=" ";
$employee_email=" ";
$employee_address=" ";
$employee_email_display=" ";
$employee_address_display=" ";
$employee_language=" ";
$employee_language_display=" ";

// when click on the submit button

if(isset($_REQUEST["btn_display"]))

{
// employeedent name validation coding…..
if (empty($_POST["txt_emp_name"]))
{
$employee_name = "please enter Name...";
}

else

{
$employee_name_display =$_POST["txt_emp_name"];
// only alphabets & white space are required….
if (!preg_match("/^[a-zA-Z ]*$/", $employee_name_display))
{

$employee_name = "Only Valid Alphabets & white space";

}

else

{
// employeedent name display….

$employee_name_display =$_POST["txt_emp_name"];
}
}

// employeedent email id validation coding…..
if (empty($_POST["txt_emp_email"]))
{
$employee_email = "please enter email id..";
}

else

{
$employee_email_display =$_POST["txt_emp_email"];
// check valid or invalid email id
if (!filter_var($employee_email_display, FILTER_VALIDATE_EMAIL))
{
$employee_email = "Invalid email format";
}

else

{

// valid email id display
$employee_email_display =$_POST["txt_emp_email"];
}

}
// employeedent address validation coding…..
if (empty($_POST["txt_emp_address"]))
{
$employee_address = "please enter address...";
}

else

{

// display employeedent address
$employee_address_display =$_POST["txt_emp_address"];
}
// employeedent selected language (radio button) validation coding...
if (empty($_POST["language"]))
{
$employee_language = "please select language.";
}

else

{
$employee_language_display = $_POST["language"];
}
}
?>

Step2: Design Page

<form method="post" action="">
<b> PHP FORM REQUIRED </b>
<table width="400" border="2">
<tr>
<td> <table width="457" border="0">
<tr>
<td> Employee Name </td>
<td> <label>
<input type="text" name="txt_emp_name" value="<?php echo $employee_name_display ;?>">
</label></td>
<td> <?php echo $employee_name; ?> </td>
</tr>
<tr>
<td> Employee Email Id </td>
<td><label>
<input type="text" name="txt_emp_email" value="<?php echo $employee_email_display ;?>">
</label></td>
<td> <?php echo $employee_email; ?></td>
</tr>
<tr>
<td> Employee Address </td>
<td>
<textarea name="txt_emp_address"> <?php echo $employee_address_display; ?> </textarea>
</td>
<td> <?php echo $employee_address; ?> </td>
</tr>
<tr>
<td> Language </td>
<td>
<input type="radio" name="language"> PHP
<input type="radio" name="language"> ASP
</td>
<td> <?php echo $employee_language; ?> </td> </tr>
<tr>
<td>
<input type="submit" name="btn_display" value="submit">
</td></tr>
</table> </td>
</tr>
</table>
</form>

Result:
PHP Form Validation

PHP Form Validation


Share:


Discount Coupons



Leave a Reply


Comments
  1. tneb online payment Jan 21, 2019

    Amazing! Its actually amazing post, I have got much clear idea on the topicof from this article.

  2. Uppada Pattu Sarees Jan 21, 2019

    Fabulous, what a blog it is! This weblog presents helpful data to us,keep it up.