Google News
logo
PHP Form Validation
The PHP Form Validation means check the input submitted by the user. Since our web application allows outsiders to enter data, server-side form validation is also required for additional security. 

We are going to see how to validate a form using PHP. We have a registration form containing name, password, email and gender fields etc. All the fields are mandatory and should not empty. The email should be in correct format. We are going to check these validations in server side.

Type the following code in "registartion.php" file and save in your project root directory :
Example :
<!DOCTYPE html>
<html>
<head>
<title>Registration Form Using PHP</title>
<style type="text/css">
.table_bg{
	background-color: #0099da;
	color:#FFF;
	font-weight:bold;
	border:1px #0099da solid;
}
.main_border{ border:2px solid #0099da;}
.table_row {
	background-color:#F60;
	color:#FFF;
	border:1px #0099da solid;
}
.title{ 
	font-size:20px;
	font-style:italic;
}
.validation_mesg{
	color: #FF0000;
	font-weight: bold;
	text-align: center;
	width: 100%;
	font-size:18px; 
	font-style:italic;
	padding: 10px;
}
.cus_form{
	padding:5px;
	border-radius:2px;
	}
.cus_btn{
	padding:6px 15px;
	border-radius:3px;
	background:#0099da;
	color:#FFF;
	}
.cus_btn:hover{
	background:#069;
}
</style>
</head>
<body>


<!-- Start PHP Validation Code -->
<?php
if(count($_POST)>0) {
foreach($_POST as $key=>$value) {
if(empty($_POST[$key])) {
$message = ucwords($key) . " field is required";
break;
}
}

/* Email ID Validation */
if(!isset($message)) {
if (!filter_var($_POST["Email_ID"], FILTER_VALIDATE_EMAIL)) {
$message = "Invalid Email ID";
}
}

/* Gender Validation */
if(!isset($message)) {
if(!isset($_POST["gender"])) {
$message = " Gender field is required";
}
}

/* Checkbox Validation */
if(!isset($message)) {
if(!isset($_POST["terms"])) {
$message = " Check the box to accept Terms and conditions";
}
}

if(!isset($message)) {
$message = "You have registered successfully!";	
unset($_POST);
}

}
?>
<!-- End PHp Validation Code -->




<form name="frmRegistration" method="post" action="">
<div align="center" class="validation_mesg"><?php if(isset($message)) echo $message; ?></div>
<table border="0" cellpadding="10" cellspacing="1" width="400" align="center" class="main_border">
<tr class="table_bg">
<td align="center" colspan="2" class="title">Registration Form</td>
</tr>
<tr class="table_row">
<td align="right">Username</td>
<td><input type="text" name="userName" class="cus_form" value="<?php if(isset($_POST['userName'])) echo $_POST['userName']; ?>"></td>
</tr>
<tr class="table_row">
<td align="right">Password</td>
<td><input type="password" name="password" class="cus_form" value="<?php if(isset($_POST['password'])) echo $_POST['password']; ?>"></td>
</tr>
<tr class="table_row">
<td align="right">Mobile Number</td>
<td><input type="number" name="MobileNumber" class="cus_form" value="<?php if(isset($_POST['MobileNumber'])) echo $_POST['MobileNumber']; ?>"></td>
</tr>
<tr class="table_row">
<td align="right">Email</td>
<td><input type="text" name="Email_ID"  class="cus_form"value="<?php if(isset($_POST['Email_ID'])) echo $_POST['Email_ID']; ?>"></td>
</tr>
<tr class="table_row">
<td align="right">Gender</td>
<td><input type="radio" name="gender" value="Male" <?php if(isset($_POST['gender']) && $_POST['gender']=="Male") { ?>checked<?php  } ?>> Male
<input type="radio" name="gender" value="Female" <?php if(isset($_POST['gender']) && $_POST['gender']=="Female") { ?>checked<?php  } ?>> Female
</td>
</tr>
<tr class="table_row">
<td align="right">Address</td>
<td><textarea rows="3" cols="30" class="cus_form" name="Address"><?php if(isset($_POST['Address'])) echo $_POST['Address']; ?></textarea></td>
</tr>
<tr class="table_row">
<td align="right"></td>
<td><input type="checkbox" name="terms"> I accept Terms and Conditions</td>
</tr>

<tr class="table_row">
<td align="center" colspan="2"><input type="submit" class="cus_btn" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>