PHP Form Validation

PHP form validation is used in many web pages. When a user entered the value in the textbox but entered value is invalid, then an error message will be displayed on the webpage. For e.g. when user type wrong email id, type special character in a name column.

How PHP Form Required Field Work?

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 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"];

}

}

?>

Step 2: 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-validation1

php-validation2


3
Leave a Reply

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Uppada Pattu Sareesontimeinfotechtneb online payment Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
tneb online payment
Guest

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

Coding Tag
Admin

Boss, we always ready to serve you and time to time upload helpful tutorials..

Uppada Pattu Sarees
Guest

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