About this

A class developed with a purpose for validating a form or an array of data based on defined rules using PHP. The constructor of this class requires two arrays while evoked those are explaining the rules and corresponding messages those are implications of particular errors found applying the rules. The Method IsValid() takes a named array (e.g. data array) as parameter and parse it applying the rules formerly set by the predefinition of the constructor mentioned earlier. Having the Data parsed it returns the number of errors if found any along with the index of data arrays those are erroneous in the verdict of given rules. It also returns the set of error message as well. It Is indeed a replication for server side validation in PHP savouring the functionality of the validator plug-in of jQuerry. Download the class and the example files from here.

A Quick Example

Below is a quick and easy example of server side php form validation containing four text box named as v1,v2,v3 and v4 with some rules like required, min length and max length, equal length, specific word etc. Later on php action script it shows an OOP Form Validation Example with the form validation class.

The form code
<form method="POST" action="submit.php">
    <table border="1" width="100%">
            <td>Enter a value (v1)</td>
            <td><input type="text" name="v1" size="20"></td>
            <td>Enter a value from 2 to 4 character long (v2)</td>
            <td><input type="text" name="v2" size="20"></td>
            <td>Enter a value exactly 8 Characters long  (v3) </td>
            <td><input type="text" name="v3" size="20"></td>
            <td>Enter 'MyBoy' (v4)</td>
            <td><input type="text" name="v4" size="20"></td>
    <p><input type="submit" value="Check" name="action"><input type="reset" value="Reset" name="B2"></p>

The submit page code
require "class.validator.inc";
    $data = $_POST;
$rules = array(
      "v3" => array(
            "eqlength"=> 8
    "v4" => array(
        "equal"    =>"MyBoy"
$message = array(
      "v1"=>"This field can not be empty",
        "require"=>"This field is required",
        "minlength"=>"String is too short",
        "maxlength"=>"Input is more character then required"
    "v4"=>array("equal"=>"I dont like this word. Please Type a good word "),
    "v3"=> array("eqlength"=>"Are you blind.")
$objValidator = new Validator($rules,$message);
    $errors = $objValidator->ErrorFields();
            echo "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: ";
        $value = $objValidator->getErrors($var);
            echo implode(" * ",$value);
        else echo $value;
        echo "<br/>";
    echo "<br/><hr/>";
else echo "valid";

View this example online click here

License and terms of use

This software is licensed under GNU GENERAL PUBLIC LICENSE, Version 1.0, August 2009

Copyright (C) 2009 Md. Monjur-ul-Hasan, Lecturer, Dept of Computer Science & Technology, Chittagong University of Engineering & Technology, Chittagong - 4349,Bangladesh. Its a freeware  to copy and redistribute verbatim copies of this class , but the authors copyright is to be included within all the upgraded version of this class.

This class is free to all aspects you use. You can use, modify and update to meet your requirements. If you distribute copies of this Class, whether gratis or for a fee, The consecutive redistributes through all its up gradation should inherit the freeware term of use as of mentioned above.  Ensure that the recipients of redistributions receive the source code and have the freeware terms of use. You should  also ensure that every copy of this class that you distribute must contain the name of the original author and the other author that contribute to the modification of  this class. As this is a free class so THERE IS NO WARRANTY FOR THIS CLASS.


Tutorial :

Example :

About The Author

Other Classes

Bookmark Us