html form validation using php?

Server Side Form Validations in php.

File Name:- index.php

<?php
if(isset($_POST['Submit'])){

$emp_name=trim($_POST["emp_name"]);
$emp_number=trim($_POST["emp_number"]);
$emp_email=trim($_POST["emp_email"]);

if($emp_name =="") {
$errorMsg= "error : You did not enter a name.";
$code= "1" ;
}

elseif($emp_number == "") {
$errorMsg= "error : Please enter number.";
$code= "2";
}

//check if the number field is numeric
elseif(is_numeric(trim($emp_number)) == false){
$errorMsg= "error : Please enter numeric value.";
$code= "2";
}

elseif(strlen($emp_number)<10){
$errorMsg= "error : Number should be ten digits.";
$code= "2";
}

//check if email field is empty
elseif($emp_email == ""){
$errorMsg= "error : You did not enter a email.";
$code= "3";
} //check for valid email
elseif(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $emp_email)){
$errorMsg= 'error : You did not enter a valid email.';
$code= "3";
}

else{
echo "Success";
//final code will execute here.
}

}
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Employee Information Systems</title>
</head>

<body>



<style type="text/css">
.errorMsg {
border: 1px solid red;
}

.message {
color: red;
font-weight: bold;
}
</style>

<form name="registration" id="registration" method="post" action="">
<table width="400" border="0" align="center" cellpadding="4"
cellspacing="1">

<tr>
<td align="center"><?php if (isset($errorMsg))
{
echo "<p class='message'>" .$errorMsg. "</p>" ;

}
?>
</td>

</tr>

<tr>
<td>Employee Name:</td>
<td><input name="emp_name" type="text" id="emp_name"
value="<?php if(isset($name)){echo $name;} ?>"
<?php if(isset($code) && $code == 1){echo "class=errorMsg" ;} ?>></td>
</tr>
<tr>
<td>Contact No.:</td>
<td><input name="emp_number" type="text" id="emp_number"
value="<?php if(isset($number)){echo $number;} ?>"
<?php if(isset($code) && $code == 2){echo "class=errorMsg" ;}?>></td>
</tr>
<tr>
<td>Personal Email:</td>
<td><input name="emp_email" type="text" id="emp_email"
value="<?php if(isset($email)){echo $email; }?>"
<?php if(isset($code) && $code == 3){echo "class=errorMsg" ;}?>></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>


Server Side Form Validations Example in php.

File Name:- index.php

<!--registration.php-->

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Employee Information Sample HTML Form</title>
</head>

<body>

<?php if (isset($errorMsg)) { echo "<p class='message'>" .$errorMsg. "</p>" ;} ?>

<style type="text/css" >
.errorMsg{border:1px solid red; }
.message{color: red; font-weight:bold; }
</style>

<form name= "registration" id= "registration" method= "post" action= "" >
<table width= "400" border= "0" align="center" cellpadding= "4" cellspacing= "1">
<tr>
<td>Employee Name:</td>
<td><input name= "emp_name" type= "text" id="emp_name" value="<?php if(isset($name)){echo $name;} ?>"
<?php if(isset($code) && $code == 1){echo "class=errorMsg" ;} ?> ></td>
</tr>
<tr>
<td>Contact No.: </td>
<td><input name= "emp_number" type= "text" id= "emp_number" value="<?php if(isset($number)){echo $number;} ?>"
<?php if(isset($code) && $code == 2){echo "class=errorMsg" ;}?> ></td>
</tr>
<tr>
<td> Personal Email: </td>
<td><input name= "emp_email" type= "text" id= "emp_email" value="<?php if(isset($email)){echo $email; }?>"
<?php if(isset($code) && $code == 3){echo "class=errorMsg" ;}?> ></td>
</tr>
<tr>
<td> </td>
<td><input type= "submit" name= "Submit" value= "Submit" ></td>
</tr>
</table>
</form>
</body>
</html>
Capturing and Validating the Form Contents



Let’s look at the PHP required for validating the submitted form. All of this code would be placed towards the top of the page before the HTML for the form:

<?php
if(isset($_POST['Submit'])){

$emp_name=trim($_POST["emp_name"]);
$emp_number=trim($_POST["emp_number"]);
$emp_email=trim($_POST["emp_email"]);

if($emp_name =="") {
$errorMsg= "error : You did not enter a name.";
$code= "1" ;
}

elseif($emp_number == "") {
$errorMsg= "error : Please enter number.";
$code= "2";
}

//check if the number field is numeric
elseif(is_numeric(trim($emp_number)) == false){
$errorMsg= "error : Please enter numeric value.";
$code= "2";
}

elseif(strlen($emp_number)<10){
$errorMsg= "error : Number should be ten digits.";
$code= "2";
}

//check if email field is empty
elseif($emp_email == ""){
$errorMsg= "error : You did not enter a email.";
$code= "3";
} //check for valid email
elseif(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $emp_email)){
$errorMsg= 'error : You did not enter a valid email.';
$code= "3";
}

else{
echo "Success";
//final code will execute here.
}

}
?>


Example 3.

File Name:- index.php

<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN" "http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.ittutorial.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server Side Form validations Using PHP - Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style type="text/css">
<?php
if(isset($error))
{
?>
input:focus
{
border:solid red 1px;
}
<?php
}
?>
</style>
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<?php
if(isset($error))
{
?>
<tr>
<td id="error"><?php echo $error; ?></td>
</tr>
<?php
}
?>
<tr>
<td><input type="text" name="uname" placeholder="User Name" value="<?php if(isset($uname)){echo $uname;} ?>" <?php if(isset($code) && $code == 1){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="text" name="email" placeholder="Your Email" value="<?php if(isset($email)){echo $email;} ?>" <?php if(isset($code) && $code == 2){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="text" name="mno" placeholder="Mobile No" value="<?php if(isset($mno)){echo $mno;} ?>" <?php if(isset($code) && $code == 3){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" <?php if(isset($code) && $code == 4){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>


Example 4.

File Name:- index.php

<!DOCTYPE HTML>
<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") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}

if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>


PHP Server side validation.

File name : index.php

<?php
if(isset($_POST['Submit'])){

$emp_name=trim($_POST["emp_name"]);
$emp_number=trim($_POST["emp_number"]);
$emp_email=trim($_POST["emp_email"]);

if($emp_name =="") {
$errorMsg= "error : You did not enter a name.";
$code= "1" ;
}

elseif($emp_number == "") {
$errorMsg= "error : Please enter number.";
$code= "2";
}

//check if the number field is numeric
elseif(is_numeric(trim($emp_number)) == false){
$errorMsg= "error : Please enter numeric value.";
$code= "2";
}

elseif(strlen($emp_number)<10){
$errorMsg= "error : Number should be ten digits.";
$code= "2";
}

//check if email field is empty
elseif($emp_email == ""){
$errorMsg= "error : You did not enter a email.";
$code= "3";
} //check for valid email
elseif(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $emp_email)){
$errorMsg= 'error : You did not enter a valid email.';
$code= "3";
}

else{
echo "Success";
//final code will execute here.
}

}
?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Employee Information Sample HTML Form</title>
</head>

<body>






<?php if (isset($errorMsg)) { echo "<p class='message'>" .$errorMsg. "</p>" ;} ?>

<style type="text/css" >
.errorMsg{border:1px solid red; }
.message{color: red; font-weight:bold; }
</style>

<form name= "registration" id= "registration" method= "post" action= "" >
<table width= "400" border= "0" align="center" cellpadding= "4" cellspacing= "1">
<tr>
<td>Employee Name:</td>
<td><input name= "emp_name" type= "text" id="emp_name" value="<?php if(isset($name)){echo $name;} ?>"
<?php if(isset($code) && $code == 1){echo "class=errorMsg" ;} ?> ></td>
</tr>
<tr>
<td>Contact No.: </td>
<td><input name= "emp_number" type= "text" id= "emp_number" value="<?php if(isset($number)){echo $number;} ?>"
<?php if(isset($code) && $code == 2){echo "class=errorMsg" ;}?> ></td>
</tr>
<tr>
<td> Personal Email: </td>
<td><input name= "emp_email" type= "text" id= "emp_email" value="<?php if(isset($email)){echo $email; }?>"
<?php if(isset($code) && $code == 3){echo "class=errorMsg" ;}?> ></td>
</tr>
<tr>
<td> </td>
<td><input type= "submit" name= "Submit" value= "Submit" ></td>
</tr>
</table>
</form>
</body>
</html>



Example

File name : index.php


<?php

if(isset($_POST['btn-signup']))
{
$uname = trim($_POST['uname']);
$email = trim($_POST['email']);
$upass = trim($_POST['pass']);
$mno = trim($_POST['mno']);

if(empty($uname))
{
$error = "enter your name !";
$code = 1;
}
else if(!ctype_alpha($uname))
{
$error = "letters only !";
$code = 1;
}
else if(empty($email))
{
$error = "enter your email !";
$code = 2;
}
else if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
{
$error = "not valid email !";
$code = 2;
}
else if(empty($mno))
{
$error = "Enter Mobile NO !";
$code = 3;
}
else if(!is_numeric($mno))
{
$error = "Numbers only !";
$code = 3;
}
else if(strlen($mno)!=10)
{
$error = "10 characters only !";
$code = 3;
}
else if(empty($upass))
{
$error = "enter password !";
$code = 4;
}
else if(strlen($upass) < 8 )
{
$error = "Minimum 8 characters !";
$code = 4;
}
else
{
?>
<script>
alert('success');
document.location.href='index.php';
</script>
<?php
}
}
?>

<!DOCTYPE html PUBLIC "-//ittutorialC//DTD XHTML 1.0 Transitional//EN" "http://www.ittutorial.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.ittutorial.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server Side Form validations Using PHP - Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style type="text/css">
<?php
if(isset($error))
{
?>
input:focus
{
border:solid red 1px;
}
<?php
}
?>
</style>
</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<?php
if(isset($error))
{
?>
<tr>
<td id="error"><?php echo $error; ?></td>
</tr>
<?php
}
?>
<tr>
<td><input type="text" name="uname" placeholder="User Name" value="<?php if(isset($uname)){echo $uname;} ?>" <?php if(isset($code) && $code == 1){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="text" name="email" placeholder="Your Email" value="<?php if(isset($email)){echo $email;} ?>" <?php if(isset($code) && $code == 2){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="text" name="mno" placeholder="Mobile No" value="<?php if(isset($mno)){echo $mno;} ?>" <?php if(isset($code) && $code == 3){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" <?php if(isset($code) && $code == 4){ echo "autofocus"; } ?> /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

style.css

File name : index.php

*
{
margin:0;
padding:0;
}
#login-form
{
margin-top:70px;
}
table
{
border:solid #dcdcdc 1px;
padding:25px;
box-shadow: 0px 0px 1px rgba(0,0,0,0.2);
}
table tr,td
{
padding:15px;
}
table tr td input
{
width:97%;
height:45px;
border:solid #e1e1e1 1px;
border-radius:3px;
padding-left:10px;
font-family:Verdana, Geneva, sans-serif;
font-size:16px;
background:#f9f9f9;
transition-duration:0.5s;
box-shadow: inset 0px 0px 1px rgba(0,0,0,0.4);
}

table tr td button
{
width:100%;
height:45px;
border:0px;
background:rgba(12,45,78,11);
background:-moz-linear-gradient(top, #595959 , #515151);
border-radius:3px;
box-shadow: 1px 1px 1px rgba(1,0,0,0.2);
color:#f9f9f9;
font-family:Verdana, Geneva, sans-serif;
font-size:18px;
font-weight:bolder;
text-transform:uppercase;
}
table tr td button:active
{
position:relative;
top:1px;
}
table tr td a
{
text-decoration:none;
color:#00a2d1;
font-family:Verdana, Geneva, sans-serif;
font-size:18px;
}

#error
{
color: brown;
font-family:Verdana, Geneva, sans-serif;
font-weight:bolder;
text-transform:capitalize;
}





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here