Login   Register  
PHP Classes
elePHPant
Icontem

File: example5.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  >  example5.php  >  Download  
File: example5.php
Role: Example script
Content type: text/plain
Description: Custom Rule
Class: Form Validation
Validate submitted form values according to rules
Author: By
Last change:
Date: 4 years ago
Size: 2,745 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.extvalidator.inc";
 
 
// if form is posted  
if($_POST['action']=="Check")
 {
    
$data $_POST["data"];
// define the rules
$rules = array(
    
"date1"=>"date",      
    
"date2"=>array(
        
"LimAge"=>array(        // LimAge is an extended rules for check
                
"format"=>"ddmmyyyy"// other properties for this LimAge
                
"min"=>18,
                
"max"=>28
                
)           
         ),
);

// define the error message
$message = array(
    
"date1"=>"please enter this value",
    
"date2"=>array(
        
"LimAge"=>"Invalid"
    
),
);

    
// create the instance of the extended validator
    
$objValidator = new ExtValidator($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";
}

?>

<form method="POST" action="">
    <p align="center">You need to be in the age of 18 to 28</p>
    <table border="1" width="100%">
        <tr>
            <td>Enter your birth date </td>
            <td><input type="text" name="data[date2]" value="<?php echo $data["date2"?>" size="20"> (dd/mm/yyyy )</td>
        </tr>
        <tr>
            <td>Enter your marriage Date</td>
            <td><input type="text" name="data[date1]" value='<?php echo $data["date1"?>'  size="20"> (dd/mm/yy)</td>
        </tr>
        </table>
    <p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>
</form>