This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Sunday 15 April 2012

PHP Validation for complete SignUp form

For PHP validation I am using the same regular expression rules as I have used and described in detail during java script validation for complete signup form. Below is complete PHP validation code. Copy and paste it in your text editor and save it with PHP extension (e.g. validation.php). Then open it in localhost server you will see it working well.
Code bellow has two parts:
  1. HTML which contains html form which you can see on your browser
  2. PHP which get user data using the globle variable $_POST and validates it using the regular expression
<?php
if($_POST['submit'] == 'Submit')
{
//If user click on submit button we get value by using $_POST where names in sequre brackets are the names of form's input fields.
$username = $_POST['username'];
$email = $_POST['email'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$password = $_POST['password'];
$passconf = $_POST['passconf'];

// Now you check if $username is empty you assign coresponding error message to an associative array named $error. You can display error message for your user using this array in HTML part.
if($username=='')
{
$error['username']= 'Please enter username <br>';
}

else if(!ereg("^[a-zA-Z0-9_]{3,16}$", $username))
{
// else codition checks if username not contains alpha-numeric characters and it is less than 3 characters or more than 16 characters. It assign error message to $error array. Validate similaely other data email, phone etc.
$error['username']= 'please enter valid username<br>';

}

if($email=='')
{
$error['email']= 'Please enter email <br>';
}
else if(!ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $email))
{
$error['email']= 'please enter valid email<br>';
}

if($password=='')
{
$error['password']= 'Please enter password<br>';
}
else if(strlen($password)<6)
{
$error['password']= 'Password is too short.<br>';
}
if($passconf=='')
{
$error['passconf']= 'Please confirm password<br>';
}
else if($password!=$passconf)
{
$error['passconf']= 'password does not match<br>';
}

if($firstname=='')
{
$error['firstname']= 'Please enter firstname<br>';
}
else if(!ereg("^[a-zA-Z]{3,16}$", $firstname))
{
$error['firstname']= 'please enter valid firstname<br>';
}
if($lastname=='')
{
$error['lastname']= 'Please enter lastname<br>';
}
else if(!ereg("^[a-zA-Z]{3,16}$", $lastname))
{
$error['lastname']= 'please enter valid lastname<br>';
}
if($phone=='')
{
$error['phone']= 'Please enter phone<br>';
}
else if(!ereg("^[0-9]{3,16}$", $phone))
{
$error['phone']= 'please enter valid phone<br>';
}
 //if error not exist store data into database or send to email server.
 if(count($error)==0)
 {
  // You can see following links for storing data into database.
  // http://b2atutorials.blogspot.com/2012/05/how-to-save-sign-up-form-data-in.html
  // http://b2atutorials.blogspot.com/2013/06/how-to-store-data-into-database-easily.html
 }
}
?>
<html>
<body>
<form name="signup" action="" method="post">
<ul>
<li>
<p>Username:</p>
<p><?php
//Now check if $error['username'] is not empty display the error message you have assign it above.
if(!empty($error['username'])) echo $error['username']; ?></p>
<p><input type="text" name="username" value="<?php if(!empty($username)) echo $username ?>" size="50" /></p>
</li>
<li>
<p>Email:</p>
<p><?php if(!empty($error['email'])) echo $error['email']; ?></p>
<p><input type="text" name="email" value="<?php if(!empty($email)) echo $email ?>" size="50" /></p></li>
<li><p>Password:</p>
<p><?php if(!empty($error['password'])) echo $error['password']; ?></p>
<p><input type="password" name="password" value="<?php if(!empty($password)) echo $password ?>" size="50" /></p></li>
<li>
<p>Password Confirm:</p>
<p><?php if(!empty($error['passconf'])) echo $error['passconf']; ?></p>
<p><input type="password" name="passconf" value="<?php if(!empty($passconf)) echo $passconf ?>" size="50" /></p></li>
<li>
<p>First Name:</p>
<p><?php if(!empty($error['firstname'])) echo $error['firstname']; ?></p>
<p><input type="text" name="firstname" value="<?php if(!empty($firstname)) echo $firstname ?>" size="50" /></p></li>
<li>
<p>Last Name:</p>
<p><?php if(!empty($error['lastname'])) echo $error['lastname']; ?></p>
<p><input type="text" name="lastname" value="<?php if(!empty($lastname)) echo $lastname ?>" size="50" /></p></li>
<li>
<p>Phone:</p>
<p><?php if(!empty($error['phone'])) echo $error['phone']; ?></p>
<p><input type="text" name="phone" value="<?php if(!empty($phone)) echo $phone ?>" size="50" /></p></li>
<li>
<p><input type="submit" name="submit" value="Submit" /></p>
</li></ul>
</form>
</body>
</html>