<?php
 
/**
 
* Validation class for validation of misc strings
 
*
 
* @author    Sven Wagener <wagener_at_indot_de>
 
* @include      Funktion:_include_
 
*/
 
class validate{
 
    var $country_code;
 
    var $string;
 
 
    /**
 
        * Regular expressions for postcode of the following countries
 
        * at = austria
 
        * au = australia
 
        * ca = canada
 
        * de = german
 
        * ee = estonia
 
        * nl = netherlands
 
        * it = italy
 
        * pt = portugal
 
        * se = sweden
 
        * uk = united kingdom
 
        * us = united states
 
        **/
 
    var $pattern_postcode=array(
 
           'at'=>'^[0-9]{4,4}$',
 
           'au'=>'^[2-9][0-9]{2,3}$',
 
           'ca'=>'^[a-zA-Z].[0-9].[a-zA-Z].\s[0-9].[a-zA-Z].[0-9].',
 
           'de'=>'^[0-9]{5,5}$',
 
           'ee'=>'^[0-9]{5,5}$',
 
           'nl'=>'^[0-9]{4,4}\s[a-zA-Z]{2,2}$',
 
           'it'=>'^[0-9]{5,5}$',
 
           'pt'=>'^[0-9]{4,4}-[0-9]{3,3}$',
 
           'se'=>'^[0-9]{3,3}\s[0-9]{2,2}$',
 
           'uk'=>'^([A-Z]{1,2}[0-9]{1}[0-9A-Z]{0,1}) ?([0-9]{1}[A-Z]{1,2})$',
 
           'us'=>'^[0-9]{5,5}[\-]{0,1}[0-9]{4,4}'
 
 
        );
 
    
 
    var $pattern_email='^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ ';
 
    var $pattern_ip='([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})';
 
    var $pattern_url='^(http|ftp)://(www\.)?.+\.(com|net|org)$';
 
    
 
    /**
 
    * The constructor of the validation class
 
    * @param string $string the string which have to be validated
 
    * @desc The constructor of the validation class
 
    */    
 
    function validate($string){
 
        $this->string=$string;
 
    }
 
 
    /**
 
    * Validates the string if it consists of alphabetical chars
 
    * @param int $num_chars the number of chars in the string
 
    * @param string $behave defines how to check the string: min, max or exactly number of chars
 
    * @desc Validates the string if it consists of alphabetical chars
 
    */    
 
    function alpha($num_chars,$behave){
 
        if($behave=="min"){
 
            $pattern="^[a-zA-Z]{".$num_chars.",}$";
 
        }else if ($behave=="max"){
 
            $pattern="^[a-zA-Z]{0,".$num_chars."}$";
 
        }else if ($behave=="exactly"){
 
            $pattern="^[a-zA-Z]{".$num_chars.",".$num_chars."}$";
 
        }
 
        return ereg($pattern,$this->string);
 
    }
 
    
 
    /**
 
    * Validates the string if it consists of lowercase alphabetical chars
 
    * @param int $num_chars the number of chars in the string
 
    * @param string $behave defines how to check the string: min, max or exactly number of chars
 
    * @desc Validates the string if it consists of lowercase alphabetical chars
 
    */    
 
    function alpha_lowercase($num_chars,$behave){
 
        if($behave=="min"){
 
            $pattern="^[a-z]{".$num_chars.",}$";
 
        }else if ($behave=="max"){
 
            $pattern="^[a-z]{0,".$num_chars."}$";
 
        }else if ($behave=="exactly"){
 
            $pattern="^[a-z]{".$num_chars.",".$num_chars."}$";
 
        }
 
        return ereg($pattern,$this->string);
 
        
 
    }
 
    
 
    /**
 
    * Validates the string if it consists of uppercase alphabetical chars
 
    * @param int $num_chars the number of chars in the string
 
    * @param string $behave defines how to check the string: min, max or exactly number of chars
 
    * @desc Validates the string if it consists of uppercalse alphabetical chars
 
    */    
 
    function alpha_uppercase($num_chars,$behave){
 
        if($behave=="min"){
 
            $pattern="^[A-Z]{".$num_chars.",}$";
 
        }else if ($behave=="max"){
 
            $pattern="^[A-Z]{0,".$num_chars."}$";
 
        }else if ($behave=="exactly"){
 
            $pattern="^[A-Z]{".$num_chars.",".$num_chars."}$";
 
        }
 
        return ereg($pattern,$this->string);
 
        
 
    }
 
    
 
    /**
 
    * Validates the string if it consists of numeric chars
 
    * @param int $num_chars the number of chars in the string
 
    * @param string $behave defines how to check the string: min, max or exactly number of chars
 
    * @desc Validates the string if it consists of numeric chars
 
    */    
 
    function numeric($num_chars,$behave){
 
        if($behave=="min"){
 
            $pattern="^[0-9]{".$num_chars.",}$";
 
        }else if ($behave=="max"){
 
            $pattern="^[0-9]{0,".$num_chars."}$";
 
        }else if ($behave=="exactly"){
 
            $pattern="^[0-9]{".$num_chars.",".$num_chars."}$";
 
        }
 
        return ereg($pattern,$this->string);
 
    }
 
    
 
    /**
 
    * Validates the string if it consists of alphanumerical chars
 
    * @param int $num_chars the number of chars in the string
 
    * @param string $behave defines how to check the string: min, max or exactly number of chars
 
    * @desc Validates the string if it consists of alphanumerical chars
 
    */        
 
    function alpha_numeric($num_chars,$behave){
 
        if($behave=="min"){
 
            $pattern="^[0-9a-zA-Z]{".$num_chars.",}$";
 
        }else if ($behave=="max"){
 
            $pattern="^[0-9a-zA-Z]{0,".$num_chars."}$";
 
        }else if ($behave=="exactly"){
 
            $pattern="^[0-9a-zA-Z]{".$num_chars.",".$num_chars."}$";
 
        }
 
        return ereg($pattern,$this->string);
 
    }
 
 
    /**
 
    * Validates the string if its a valid postcode
 
    * @param string $country_code the country code for the country of the postcode (de,en)
 
    * @desc Validates the string if its a valid postcode
 
    */    
 
    function postcode($country_code){
 
        if(array_key_exists($country_code,$this->pattern_postcode)){
 
            return ereg($this->pattern_postcode[$country_code],$this->string);
 
        }else{
 
            return false;
 
        }
 
    }
 
 
    /**
 
    * Validates the string if its a valid email address
 
    * @desc Validates the string if its a valid email adress
 
    */    
 
    function email(){
 
        return ereg($this->pattern_email,$this->string);
 
    }
 
    
 
    /**
 
    * Validates the string if its a valid ip address
 
    * @desc Validates the string if its a valid ip address
 
    */    
 
    function ip_address(){
 
        return ereg($this->pattern_ip,$ip);
 
    }
 
    
 
    /**
 
    * Validates the string if its a valid URL
 
    * @desc Validates the string if its a valid URL
 
    */    
 
    function url(){
 
        return ereg($this->pattern_url,$ip);
 
    }
 
}
 
?>
 
 |