Social Networks

Always JavaScript validation is he best practice. In almost all the web best applications we used JavaScript validation to make page fast and to give good user experience. The question is what if the JavaScript is disable. So in this post I given a simple example about “Server Side Form Validation using Regular Expressions “.

regular-expression-form-server-side-validation

Take a look at live demo.

Download Source

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>
  1. here is my code,please help me,
    0)&&(strlen($valid_email)>0)&&(strlen($valid_username)>0)&&(strlen($valid_password)>0) && $valid_gender>0 )
    {
    $sql=”INSERT INTO registration(FullName,Email,UserName,Password,Gender)VALUES(‘$name’,’$email’,’$username’,’$password’,’$gender’)”;
    mysql_query($sql,$con);
    //if(!mysql_query($sql,$con))
    //{
    // die(“could not add”.mysql_error());
    // }
    // else
    // {
    // echo”add”;
    // }
    header(“Location: thanks.html”);

    }
    else
    {
    }

    }
    ?>

    9lessons Registration Form Tutorial

    .err
    {
    font-size:11px;
    padding-left:10px;
    color:#CC0000;
    float:left;

    }
    input
    {
    float:left;

    }

    Registration Form
    More tutorials http://9lessons.blogspot.com

    Full Name

    <input id="name" name="name" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”1″ />

    Email

    <input id="email" name="email" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”2″ />

    User-ID

    <input id="username" name="username" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”3″ />

    Password

    <input id="password" name="password" type="password" class="field text medium" value="” maxlength=”255″ tabindex=”4″ />

    Gender

    GenderMaleFemale

  2. here is my code,please help me.
    0)&&(strlen($valid_email)>0)&&(strlen($valid_username)>0)&&(strlen($valid_password)>0) && $valid_gender>0 )
    {
    $sql=”INSERT INTO registration(FullName,Email,UserName,Password,Gender)VALUES(‘$name’,’$email’,’$username’,’$password’,’$gender’)”;
    mysql_query($sql,$con);
    //if(!mysql_query($sql,$con))
    //{
    // die(“could not add”.mysql_error());
    // }
    // else
    // {
    // echo”add”;
    // }
    header(“Location: thanks.html”);

    }
    else
    {
    }

    }
    ?>

    9lessons Registration Form Tutorial

    .err
    {
    font-size:11px;
    padding-left:10px;
    color:#CC0000;
    float:left;

    }
    input
    {
    float:left;

    }

    Registration Form
    More tutorials http://9lessons.blogspot.com

    Full Name

    <input id="name" name="name" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”1″ />

    Email

    <input id="email" name="email" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”2″ />

    User-ID

    <input id="username" name="username" type="text" class="field text medium" value="” maxlength=”255″ tabindex=”3″ />

    Password

    <input id="password" name="password" type="password" class="field text medium" value="” maxlength=”255″ tabindex=”4″ />

    Gender

    GenderMaleFemale

    • IPhone app market is a covtpeitime industry, but it can also be a rewarding experience. With hundreds and thousands of applications available in the iPhone, it can be quite difficult to compete. This fact should not discourage new developers themselves. After all, the demand for these applications always been high. With current technology and the availability of tools for creating iPhone apps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>