PHP Classes
elePHPant
Icontem

File: example2.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of M H Rasel  >  Form Validation  >  example2.php  >  Download  
File: example2.php
Role: Example script
Content type: text/plain
Description: Date & Email Example
Class: Form Validation
Validate submitted form values according to rules
Author: By
Last change:
Date: 5 years ago
Size: 3,057 bytes
 

Contents

Class file image Download
<?php
/**
  * -------------------------------------------------------------------------
  * PHP Form Validator (formvalidator.php)
  * Version 1.0
  * -------------------------------------------------------------------------
  * @author M H Rasel
  * 4-5-2009::0:15
  * @copyright 2009
  * email:mail@monjur-ul-hasan.info
  * Documentation: http://phpformvalidation.monjur-ul-hasan.info
 */
 
require "class.validator.inc";
 
// if form is posted
 
if($_POST['action']=="Check")
 {
    
//// if form is posted
   
$data = $_POST['data']; // collect the data from the form


   
$rules = array(
   
"date1"=>"date", // check the field data for valid data with default format mm/dd/yy
   
"date2"=>array(
       
"date"=>array(
               
"format"=>"ddmmyyyy" // check the field data with date format dd/mm/yyyy
               
)
         ),
   
"email"=>"email", // check the field for valid email address
);

// for date1 field if any error found then default error messege for date will be shown
$message = array(
   
"date2"=>array(
       
"format"=>"Unsupported Data format" // if the date is not valid according to the given format this error messee will sho
   
),
   
"email"=>"Invalid Email Address", // if email address is not correct then this messege will shown
);

// create the instance
$objValidator = new Validator($rules,$message);

echo
"<h2><a href='#'>Validator Result </a></h2>";
// call the validation proce
if(!$objValidator->isValid($data))
{
   
// error found
   
    // get the list of form fields which have errorful data
   
$errors = $objValidator->ErrorFields();
   
// show the number of error
   
echo "<br/> you have total ".$objValidator->CountError()." errors.<br/>Fields are :".implode(",",$errors);
    echo
"<br/><hr/>";
   
    foreach(
$errors as $key=>$var)
    {
        echo
"For the field <b>$var</b> errors are: ";
       
// collect error for each error full field
       
$value = $objValidator->getErrors($var);
       
        if(
is_array($value))
           
// if the field have more then one validation rule faild then an array will return
           
echo implode(" * ",$value);
        else echo
$value;
        echo
"<br/>";
    }
    
}
else echo
"valid";
echo
"<br/><br/>";

?>
<h2> Fill The Following Form </h2>
<form method="POST" action="">
    <table border="1" width="100%">
        <tr>
            <td>Enter a date (in default date format - mm/dd/yy )</td>
            <td><input type="text" name="data[date1]" size="20"></td>
        </tr>
        <tr>
            <td>Enter a date (in dd/mm/yyyy formate)</td>
            <td><input type="text" name="data[date2]" size="20"></td>
        </tr>
        <tr>
            <td>Email: </td>
            <td><input type="text" name="data[email]" size="20"></td>
        </tr>
    </table>
    <p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>
</form>