Some days back I had posted " Perfect Javascript Form Validation using RegularExpressions ", one of the reader commented like this "If I disable Javascript?". So in this post I given a simple example about "Server Side Form Validation using Regular Expressions ". Take a look at live demo.
Download Script Live Demo
validation.php
Contains PHP code. eregi — Case insensitive regular expression match.
<?php
if($_POST){
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
// Full Name
if (eregi('^[A-Za-z0-9 ]{3,20}$',$name)){
$valid_name=$name;
}
else
{
$error_name='Enter valid Name.';
}
// Email
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email)){
$valid_email=$email;
}
else
{
$error_email='Enter valid Email.';
}
// Usename min 2 char max 20 char
if (eregi('^[A-Za-z0-9_]{3,20}$',$username)){
$valid_username=$username;
}
else
{
$error_username='Enter valid Username min 3 Chars.';
}
// Password min 6 char max 20 char
if (eregi('^[A-Za-z0-9!@#$%^&*()_]{6,20}$',$password)){
$valid_password=$password;
}
else
{
$error_password='Enter valid Password min 6 Chars.';
}
// Gender
if ($gender==0){
$error_gender='Select Gender';
}
else
{
$valid_gender=$gender;
}
if((strlen($valid_name)>0)&&(strlen($valid_email)>0)
&&(strlen($valid_username)>0)&&(strlen($valid_password)>0) && $valid_gender>0 )
{
mysql_query(" SQL insert statement ");
header("Location: thanks.html");
}
else{ }
}
?>
index.php
Contains HTML code. You have to include validation.php file.
<php include("validation.php"); ?>
<form method="post" action="" name="form">
Full name : <input type="text" name="name" value="<?php echo $valid_name; ?>" />
<?php echo $error_name; ?>
Email : <input type="text" name="name" value="<?php echo $valid_email; ?>" />
<?php echo $error_email; ?>
Username : <input type="text" name="name" value="<?php echo $valid_username; ?>" />
<?php echo $error_username; ?>
Password : <input type="password" name="name" value="<?php echo $valid_password; ?>" />
<?php echo $error_password; ?>
Gender : <select name="gender">
<option value="0">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
<?php echo $error_gender; ?>
</form>
<form method="post" action="" name="form">
Full name : <input type="text" name="name" value="<?php echo $valid_name; ?>" />
<?php echo $error_name; ?>
Email : <input type="text" name="name" value="<?php echo $valid_email; ?>" />
<?php echo $error_email; ?>
Username : <input type="text" name="name" value="<?php echo $valid_username; ?>" />
<?php echo $error_username; ?>
Password : <input type="password" name="name" value="<?php echo $valid_password; ?>" />
<?php echo $error_password; ?>
Gender : <select name="gender">
<option value="0">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
<?php echo $error_gender; ?>
</form>
Related posts :
Submit a Form without Refreshing page with jQuery and Ajax.Perfect Javascript Form Validation using RegularExpressions
if((strlen($valid_name)>0)&&(strlen($valid_email)>0)
ReplyDelete&&(strlen($valid_username)>0)&&(strlen($valid_password)>0) && $valid_gender>0 )
{
mysql_query(" SQL insert statement ");
header("Location: thanks.html");
}
else{ }
}
You don't need else {}
its cool, @Srinivass all the credits for you man!, hope more interesting topics will come on this blogger!
ReplyDeleteNice article. But u can use preg_match() which is fast.
ReplyDeleteyes, use preg_match(). It is not only faster, but ereg will probably not be included as standard in future PHP versions
ReplyDeleteDude you go way out of your way on this.
ReplyDeleteput the regexs into an array
$regs['name'] = "..*"; etc....
then loop through the array
foreach( $regs as $k => $v )
then
if( ! ereg( $v , $_POST[$k] ) )
{
$errors[$k] = "Invalid!";
}
use a loop luke!
Also store the error text in another array of string if you want to display text according to the error...
ReplyDeleteIt's nice and clean, but I would suggest to add some lines to keep the gender choice in case of validation not passed.
ReplyDeleteThanks for sharing this.
Panamon Rn+
I do believe that it would be more appropriate to use filter functions instead.
ReplyDeleteRegards,
Márcio
with the name you can't use double barrels carl john-joe for eg
ReplyDeleteGreat tutorial.
ReplyDeleteplease tell me details with an example
ReplyDeletemysql_query(" SQL insert statement ");
I do have same concept as this one but this one made it clear... nice job
ReplyDeletei want generalised pattern for writing password..
ReplyDeletelike ram@143 or 1@ram43 0r 1ram3@
the expression should contain @,alphabets and numbers compulsorily..
can anyone help me???
sir prao_math()
ReplyDeleteuse it please !
kindly use prag_math() please
ReplyDeletekindly use prag_math() please
ReplyDeleteThere is something I don't understand, where is your submit button to submit the form ? Also, why do you echo the valid variable ?
ReplyDeleteThanks
super
ReplyDeletesuper tutorial
ReplyDeleteGreat one
ReplyDeleteThis is very nice tutorial..Thnx..
ReplyDeleteThis is very nice tutorial..Thnx..
ReplyDeleteI have done all but i get this:
ReplyDeleteUndefined variable: valid_name in C:\wamp\www\pf\server-side.php on line 128 Call Stack #TimeMemoryFunctionLocation 10.0011381872{main}( )..\server-side.php:0 " maxlength="255" tabindex="1" />
You can enter names as numbers, not sure if that's a good idea
ReplyDeletegood job my friends i like it............
ReplyDeleteGood job my friend i like it.........
ReplyDeleteany one tell me.. when i select a option(drop-down) press submit.. after submit it will show which one i select dropdown that set selected.. how can i do this help any one please......
ReplyDeleteit was very helpful.... thank you...
ReplyDeletehow about making it post to a file like process.php instead of redirecting to a thankyou.htm page?
ReplyDeletehow to validate form with database
ReplyDeletenice & clean for beginner..
ReplyDeletegood one for the trinees
ReplyDeletegood one for the beginers
ReplyDeletenice work man,keep it up
ReplyDeleteHI,
ReplyDeleteDeprecated: Function eregi() is deprecated in C:\wamp\www\RegistrationAndLogin\validation.php on line 10
this is my code
thanx!!
ReplyDeletehmmmm
ReplyDeleteThis is very nice tutorial..Thnx..
ReplyDeleteThis is very nice tutorial..Thnx..
ReplyDeletei used this code but without return statement its not work for me please give me solutoin
ReplyDeleteThank you.
This is a very nice article. I love the way of using regular expression for validation. It will help the new comers in PHP.
ReplyDelete