Monday, 27 May 2013

Server Side Form Validation using php

Server Side Form Validation using php


Server Side Form Validation Demo given below.


CODE :


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

$name=trim($_POST["name"]);
$number=trim($_POST["number"]);
$email=trim($_POST["email"]);

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

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

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

elseif(strlen($number)<5) {
$error= "error : Number should be five digits.";
$code= "2";
}

//check if email field is empty
elseif($email == "" ) {
$error= "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", $email)) {
$error= 'error : You did not enter a valid email.';
$code= "3";
}

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

}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>phpedu108</title>
<style type="text/css">
#mainframe {
margin:auto;
width:600px;
background-color:#fbfbfb;
padding:10px;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333;
background-color: #FFF;
margin: 0px;
}
.error {
border:1px solid #DB4A39 !Important;
}
.message {
color: #DB4A39;
font-weight:bold;
}
.table {
background-color:#EBEBEB;
}
.table td {
background-color:#F8F8F8;
}
.table input[type="text"] {
border-color: #C0C0C0 #D9D9D9 #D9D9D9;
border-radius: 1px 1px 1px 1px;
border-style: solid;
border-width: 1px;
font-family: Verdana, Geneva, sans-serif;
font-size: 14px;
padding: 5px 6px;
width: 185px;
}
.table input[type="text"]:hover {
border-color: #A0A0A0 #B9B9B9 #B9B9B9;
border-style: solid;
border-width: 1px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1) inset;
}
.table input[type="Submit"] {
-moz-user-select: none;
background-color: #519A33;
border-color: #3B6E22 #3B6E22 #2C5115;
border-style: solid;
border-width: 1px;
box-shadow: 0 1px 0 rgba(0, 0, 0, 0.1);
color: #FFFFFF;
cursor: pointer;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 12px;
font-weight: 700;
height: 28px;
min-width: 40px;
padding: 0 16px;
text-align: center;
}
.table input[type="Submit"]:hover {
text-decoration: none;
}
.table input[type="Submit"]:active {
background-image: none;
}
h1 {
font-size: 160%;
font-weight: normal;
margin: 0 0 0.2em;
}
</style>
</head>
<body>
<div id="mainframe">
  <h1>Server Side Form Validation Demo</h1>
  <p>More tutorials <a href="www.phpedu108.blogspot.com/">http://www.phpedu108.blogspot.com/</a></p>
  <br>
  <form name= "info" id= "info" method= "post" action= "" >
    <table width= "327" border= "0" align="center" cellpadding= "5" cellspacing= "1" class="table">
      <?php if (isset($error)) { ?>
        <tr>
          <td colspan="2" align="center" ><?php echo "<p class='message'>" .$error. "</p>" ; ?></td>
        </tr>
        <?php } ?>
      <tr>
        <td width= "82" align="right" >Name: </td>
        <td width= "238" ><input name= "name" type= "text" value="<?php if(isset($name)){echo $name;} ?>" <?php if(isset($code) && $code == 1){echo "class=error" ;} ?> ></td>
      </tr>
      <tr>
        <td align="right">Number: </td>
        <td><input name= "number" type= "text" id= "number" value="<?php if(isset($number)){echo $number;} ?>"<?php if(isset($code) && $code == 2){echo "class=error" ;}?> ></td>
      </tr>
      <tr>
        <td align="right"> Email: </td>
        <td><input name= "email" type= "text" id= "email" value="<?php if(isset($email)){echo $email; }?>"<?php if(isset($code) && $code == 3){echo "class=error" ;}?> ></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type= "submit" name= "Submit" value= "Submit" /></td>
      </tr>
    </table>
  </form>
</div>
</body>
</html>

No comments:

Post a Comment