Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 1.14 KB

README.md

File metadata and controls

43 lines (35 loc) · 1.14 KB

PHP-Form-Validation

PHP Form validation class

A Simple and lightweight PHP validator class that will validate form submitted data

Usage example:

include( Validate.class.php );

// set the rules to sanitize and validate.

$rules = array( 
    'name' => array(
        'required'     => true,
        'alpha_space'  => true,
        'min_length'   => 3,
        'max_length'   => 19 ),

    'id' => array(
        'required'     => true,
        'numeric'      => true,
        'validate_int' => true ) );

// form was submitted.
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
    // Initiate a new validator using the post rules.
    $validator = new Validate( $_POST, $rules );

    // if validation passed return clean data or error.
    if( $validator->validate() )
    {
        $vars = $validator->getFields();
        // code that uses validated fields
    } else {
        $error = $validator->getError();
        // code that uses error messege
    }
}

New rule functins can be easily added by adding the functions to the Validate.class.php file and they can be called by using the same name as the function on the rules array.