PHP

Server Side form Validation in PHP

Server side validation is a another way to validate a HTML Form.In Server Side validation we can validate empty filed,input length, numeric value, valid email id and many more.
View Demo

Create a HTML From. Create Name, Email , Password and contact no field. Below Structure of the form given below. I have embed PHP code in form to display error message.

<form method="post">
<table align="center" width="50%" 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>

PHP Script
This script validate empty  input field, input length, validate email and validate numeric value. Put this code above the HTML tags

<?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
 }
}
?>
View Demo
Download Source Code (Server Side form Validation in PHP)
Size: 1 KB
Version: V 1.0

Recommended Tutorials for you


Related posts

JQuery Ajax Tutorial – Learn About Developing Effective Next Generation Web Application

Anuj Kumar

Types of Errors in PHP

Anuj Kumar

How to upload and validate a image in php

Anuj Kumar

How to get Browser Information In PHP

Anuj Kumar

PHP Prepared Statements

Anuj Kumar

Looping Statement In PHP

Anuj Kumar

Useful functions for arrays in PHP

Anuj Kumar

PHP Constants

Anuj Kumar

How to Connect PHP with MySQL Database

Anuj Kumar

1 comment

Amit March 11, 2019 at 4:47 am

Thank you for sharing.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.