Validation.php
<?php

    /**
     *   
     * Validation class
     *
     * @author      Harold Kim E. Cantil 
     * @copyright   Harold Kim E. Cantil 2007-2008
     * @version     $1.0$
     * @package     none
     * @subpackage  none
     */
    class Validation{






       /**
        *
        * class constructor
        *
        * @return   none
        * @access   public
        * @param    $dbconn - connection string
        */
        function Validation() {
        }






       /**
        *
        * checks if it's valid alpha character
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $val 
        */
        function isAlpha ( $val ){
            return preg_match('/^[a-zA-Z]+$/', $val ) ? TRUE : FALSE ;
            //return preg_match("/^[\w\-]+$/", $val) ? TRUE : FALSE ;
        }






       /**
        *
        * checks if it's a valid numeric character
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $val
        */
        function isNumeric( $val ){
            return is_numeric( $val ) ? TRUE : FALSE ;
        }







       /**
        *
        * checks if it's a valid alpha numeric character
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $val
        */
        function isAlphaNumeric( $val ) {
            return  preg_match('/^[a-zA-Z0-9]+$/', $val ) ? TRUE : FALSE ;
        }








       /**
        *
        * compares two variable
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $val1
        * @param    $val2
        */
        function compare( $val1 , $val2 ){
            return $val1 === $val2 ? TRUE : FALSE ;
        }









       /**
        *
        * determines whether the value is greater than 
        * OR less than the specified conditions
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $val - the number to be evaluated
        * @param    $max - the maximum number
        * @param    $min - the minumum number
        */
        function length( $val , $max , $min ) {
            return ( !( strlen( $val ) > $max ) && !( strlen( $val ) < $min ) )
                ? TRUE : FALSE ;
        }









       /**
        *
        * validates email address 
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $email - email address 
        */
        function isEmail( $email ) {
            $pattern = '/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])
                +(\.[a-zA-Z0-9_-]+)+/';
            return preg_match( $pattern , $email ) ? TRUE : FALSE ;
            //return  preg_match("/^[\w\-\.]+\@[\w\-\.]+$/", $email ) 
                ? TRUE : FALSE ;
        }








       /**
        *
        * check if it's a valid url address
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $url - url address
        */
        function isUrl( $url ){

            // note : use this if protocol checking is only used
            //return eregi("^(http|ftp|https)://", $url) ? TRUE : FALSE ;

            $protocol =  '((http|https|ftp)://)';
            $access = '(([a-z0-9_]+):([a-z0-9-_]*)@)?';
            $sub_domain = '(([a-z0-9_-]+\.)*)';
            $domain = '(([a-z0-9-]{2,})\.)';
            $tld  = '(com|net|xxx|org|edu|gov|mil|int|arpa|aero|biz|coop
                |info|museum|name|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at
                |au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt
                |bv|bw|by|bz|ca|cc|cf|cd|cg|ch|ci|ck|cl|cm|cn|co|cr|cs
                |cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et
                |fi|fj|fk|fm|fo|fr|fx|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp
                |gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io
                |iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz
                |la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml
                |mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf
                |ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm
                |pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si
                |sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj
                |tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va
                |vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zr|zw)';
            $port = '(:(\d+))?';
            $path = '((/[a-z0-9-_.%~]*)*)?';
            $query = '(\?[^? ]*)?';
            $extra = "$`iU";

            $pattern = "`^" .$protocol. $access . $sub_domain .
                $domain . $tld . $port . $path . $query . $extra ;
            return  preg_match( $pattern,  $url ) ? TRUE : FALSE ;

        }








       /**
        *
        * check if it's a valid url image address
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $url - url image address
        */
        function isUrlImage( $url ) {
            return eregi("^(http|ftp|https)://.+\.(jpg|jpeg|jpe|png|gif
                |bmp|wbmp|rle|dib|eps|pcx|tif|tiff)$", $url)
                ? TRUE : FALSE ;
        }









       /**
        *
        * check if it's a valid date
        *
        * @return   boolean - returns TRUE on success and FALSE on error
        * @access   public
        * @param    $data - date in "YYYY-MM-FF" format
        */
        function isDate( $date ){
            $ret = FALSE;
            $format = eregi("^[0-9]{4}-[0-9]{2}-[0-9]{2}($| 
                [0-9]{2}:[0-9]{2}$)", ( $date ) );
            if ( $format ) {
                $splitDate = split( "-" ,
                    str_replace(" ", "-", trim( $date ) ) , 4 );
                $year =  $splitDate[0];
                $month = $splitDate[1];
                $day =   $splitDate[2];

                $ret = checkdate( $month , $day , $year );
            }
            return $ret;
        }








}
?>
 
Hosted by www.Geocities.ws

1