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:
Code bellow has two parts:
- HTML which contains html form which you can see on your browser
- 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>
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>
how will u send values into database then... moving to another page.. and what if we refresh page after successfull entry..
ReplyDeleteclick the link bellow to see how to store data info database
ReplyDeletehow to store data into database easily and reset the form after successful submission