<?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")
{
$data = $_POST;
// define the rules
$rules = array(
"v1"=>"Require", // empty field is not allowed
"v2"=>array(
"require"=>true, // empty field is not allowed
"minlength"=>2, // the given data in this field should have at least 2 chatecter
"maxlength"=>4 // the given data in this field can not have more then 4 chatecter
),
"v3" => array(
"eqlength"=> 8 // the given data in this field must have 8 chatecter in length
),
"v4" => array(
"equal" =>"MyBoy" // the given data in this field should be "MyBoy"
)
);
$message = array(
"v1"=>"This field can not be empty", // if this field left empty this messege will show.
"v2"=>array(
"require"=>"This field is required", // if this field left empty this messege will show.
"minlength"=>"String is too short", // if the given data in this field have less then 2 charecter then this messege will show
"maxlength"=>"Input is more character then required" // if the given data in this field have more then 4 charecter then this messege will show
),
"v3"=> array("eqlength"=>"Are you blind?"), // if data are not 8 character long then this messege will show
"v4"=>array("equal"=>"I dont like this word. Please Type a good word ") // if the given data in this field is not "MyBoy" then this will show
);
// 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/>";
}
?>
<br/>
<h2> Fill The Following Form </h2>
<div class='articles'>
<form method="POST" action="">
<table border="1" width="100%">
<tr>
<td>Enter a value (v1)</td>
<td><input type="text" name="v1" size="20"></td>
</tr>
<tr>
<td>Enter a value from 2 to 4 character long (v2)</td>
<td><input type="text" name="v2" size="20"></td>
</tr>
<tr>
<td>Enter a value exactly 8 Characters long (v3) </td>
<td><input type="text" name="v3" size="20"></td>
</tr>
<tr>
<td>Enter 'MyBoy' (v4)</td>
<td><input type="text" name="v4" size="20"></td>
</tr>
</table>
<p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>
</form>
|