Form Validate Script

Form Validate Script is use to avoid duplicate value for a database when you insert data.

Demo :



Username:

Password (6 or more characters):

E-mail:

Age:


 

Example :

Just click Submit a button then error will display on same page with red color font otherwise if you enter correct data click and submit button of form ' Thank you for your submission' message will be display the same page.

Code :

Following code is use to Validate Form Script if you need to copy and use to your website.


<?php
if(!$_POST['submit'])
{
?>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  Username: <input type="text" name="username">
  Password (6 or more characters):
  <input type="password" name="password">
  E-mail: <input type="text" name="email">
  Age: <input type="text" name="age" size="2">
  <input type="submit" name="submit" value="Submit"> <input type="reset" name="submit" value="Cancel">
  </form>
<?php
}
else
{
   $errorList = array();
  if (!isset($_POST['username']) || trim($_POST['username']) == "")
  {
    $errorList[] = "<font style='color:#FF0000'>ERROR: Missing value 'username'</font>";
  }
  if (!is_numeric($_POST['age']))
  {
    $errorList[] = "<font style='color:#FF0000'>ERROR: Incorrect data type for value 'age'</font>";
  }
  if (strlen($_POST['password']) > 6)
  {
    $errorList[] = "<font style='color:#FF0000'>ERROR: Incorrect length for value 'password'</font>";
  }
  if (!eregi("^([a-z0-9_-]|\.)+@(([a-z0-9_-])+\.)+[a-z]{2,6}$", $_POST['email']))
  {
    $errorList[] = "<font style='color:#FF0000'>ERROR: Incorrect format for value 'email'</font>";
  }
  if (sizeof($errorList) > 0)
  {
    echo "Please review and correct the following errors: <br />";
    foreach ($errorList as $e)
    {
    echo "$e <br />";
  }
  }
  else
  {
    echo "<b>Thank you for your submission </b>  ";
  }
  echo "<input type='reset' name='submit' value='Cancel'>";
}
?>

In the above form validation script is run and validate on this same page.

Free Download Script :

If you need Form Validate Script click and download from following link.

Download




Content