PHP Classes

File: Validit.php

Recommend this page to a friend!
  Packages of Richard Hayes   Validit   Validit.php   Download  
File: Validit.php
Role: Class source
Content type: text/plain
Description: Class
Class: Validit
Validate user submitted data
Author: By
Last change: typo
Date: 23 years ago
Size: 3,691 bytes
 

 

Contents

Class file image Download
<?php
/***
* Class Validit used for validating user submitted data.
* Author:        Richard Hayes
* E-mail:        richard_c_hayes@yahoo.co.uk
* Web:            www.cuttingimage.com
* Date Created:        28-05-03
* Last Modified:    28-05-03
* Version:        1.0
* License:        GPL
***/
class Validit
{
    /***
    * Stores error messages for when the user submits invalid data
    * @type array 
    ***/
    var $errorList;

    /***
    * Constructor 
    ***/
    function Validit()
    {
        $this -> errorList = array();
    }

    /***
    * Used by validating methods as a final call, removes whitespace 
    * @param $value data for cleaning 
    * @returns void 
    ***/
    function trim(&$value)
    {
        $value = trim($value);
    }

    /***
    * Adds an error message to the array
    * @return void
    ***/
    function setError($msg)
    {
        $this -> errorList[] = $msg;
    }

    /***
    * Reseting the error list 
    * @returns void 
    ***/
    function resetErrorList()
    {
        unset($this -> errorList);
    }

    /***
    * Returns error list 
    * @returns array 
    ***/
    function getErrors()
    {
        return $this -> errorList;
    }

    /***
    * Check if string is alphabetic 
    * @param $value string to check 
    * @param $msg error message for non-alphabetic strings 
    * @param $allow additional characters to allow other than letters. For space chars use reg-ex "[:space:]"
    * @returns mixed 
    ***/
    function alphaCheck(&$value, $msg, $allow = '')
    {
        preg_match('/^[a-zA-Z'.$allow.']+$/', $value) ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Check for numeric values 
    * @param $value to check 
    * @param $msg error message for non-numeric values
    * @returns mixed 
    ***/
    function numericCheck(&$value, $msg)
    {
        is_numeric($value) ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Check for alphanumeric values 
    * @param $value to check 
    * @param $msg error message for non- alphanumeric values
    * @param $allow additional characters to allow other than integers and letters. For space chars use reg-ex "[:space:]"
    * @returns mixed 
    ***/
    function alphaNumericCheck(&$value, $msg, $allow = '')
    {
        preg_match('/^[a-zA-Z0-9'.$allow.']+$/', $value) ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Check for identical comparisons 
    * @param $value value one to compare 
    * @param $value2 value two to compare 
    * @param $msg error message for invalid email addresses 
    * @returns mixed 
    ***/
    function compareCheck(&$value, $value2, $msg)
    {
        $value ===  $value2 ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Check that field complies to max and min length restrictions
    * @param $value string to check 
    * @param $msg error message for empty strings 
    * @param $max_length maximum length the data is allowed to be 
    * @param $min_length minimum length of string 
    * @returns mixed 
    ***/
    function lengthCheck(&$value, $msg, $max_length, $min_length = 0)
    {
        !(strlen($value) > $max_length) && !(strlen($value) < $min_length) ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Check for valid email addresses 
    * @param $value email to check 
    * @param $msg error message for invalid email addresses 
    * @returns mixed 
    ***/
    function emailCheck(&$value, $msg)
    {
        $pattern = '/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/';

        preg_match($pattern, $value) ? $this -> trim($value) : $this -> setError($msg);
        return $value;
    }

    /***
    * Reseting the error list 
    * @returns void 
    ***/
    function resetErrorMessage()
    {
        unset($this -> errorList);
    }
}
?>