
<?PHP
/**
* Validate input by format.
*
* Validate your input with this simple and small class.
* I made use of Perl-compatible regular expressions,
* because they are often faster than their ereg* counterparts.
* You can use this class 'out-of-the-box'.
*
* Example usage:
* <?PHP
* require_once('validatefields.class.php');
* // No need to create an instance of the class,
* // because we use static functions (PHP5's new feature).
* $varname = 'email@address.com';
* if(validateFields::validateEmail($varname))
* { print 'Valid e-mailaddress.'; }
* else { print 'Not a valid e-mailaddress.'; }
* ?>
*
* @author Tri Pham <tri[at]tripham.nl>
* @link http://www.tripham.nl/
* @since 30 december 2004
* @version 0.1
* @todo Add more functions such as date and time functions.
* More options such as offset parameters.
*/


/**
* This is the actual code class.
*
* @since 29 december 2004
*/

class validateFields {

    
/**
* Validate input. Only characters allowed.
* @param string $pString
* @return mixed
*/
    public static function onlyCharacters($pString) {

        return(preg_match("/^[aA-zZ]+$/", $pString));
        
    }
    
/**
* Validate input. Only digits allowed.
* @param int $pDigits
* @return mixed
*/
    public static function onlyDigits($pDigits) {
        
        return(preg_match("/^[0-9]+$/", $pDigits));
        
    }
    
/**
* Validate e-mailaddress.
* @param string $pEmail
* @return mixed
*/
    public static function validateEmail($pEmail) {
        
        // The TLD (Top Level Domain) can be 2 or 4 characters long.
        // Example: .nl and .info
        
        return(preg_match("/[a-z0-9.-]+@[a-z0-9.-]+\.[a-z]{2,4}/i", $pEmail));
        
    }
    
/**
* Validate Dutch zipcodes.
* @param mixed $pZipcode
* @return mixed
*/
    public static function validateDutchZipcode($pZipcode) {

        // Validate Dutch zipcodes.
        // Valid zipcodes (without the single quotes of course):
        // '1337TP' and '1337 TP'
        
        return(preg_match("/^[0-9]{4}\s?[A-Z]{2}$/", strtoupper($pZipcode)));
        
    }
    
/**
* Validate Dutch thelephone numbers.
* @param mixed $pPhonenumber
* @return mixed
*/
    public static function validateDutchPhoneNumber($pPhonenumber) {

        // Some Dutch phonenumbers start off with 3 digits,
        // while other start off with 4 digits.
        // Valid Dutch phonenumbers: 1337-123456 and 101-1234567
        
        return(preg_match("/^(\d{3}-?\d{7}|\d{4}-?\d{6})$/", $pPhonenumber));
        
    }
    
/**
* Validate IP version 4 addresses.
* @param mixed $IPaddress
* @return mixed
*/
    public static function validateIPv4($pIPaddress) {

        // I got this regular expression from the preg_match() comments.
        
        /**
        * @var string $sPattern Our pattern.
        */
        $sPattern = "([0-9]|^1?\d\d$|2[0-4]\d|25[0-5])";
        
        return(preg_match("/$sPattern\.$sPattern\.$sPattern\.$sPattern/", $pIPaddress));
    }
    
/**
* Validate URL.
* @param string $pURL
* @return mixed
*/
    public static function validateURL($pURL) {

        // Een URL matchen is een hell!!
        
        // Valid URL's:
        // http://www.tripham.nl/
        // http://tripham.nl/
        // http://security.tripham.nl/
        // https://www.tripham.nl:80/
        // http://1337.ditbestaatniet.tripham.nl
        // http://blaat:aap@tripham.nl:80
        // The last slash is optional.
        
        // Regular expressions for traditional URL's.
        //return(preg_match("/^https?:\/\/(www|[a-z])?\.?[a-z0-9\-]+\.([a-z]{2,4}\/?)$/i", $pURL));
        
        return(preg_match("/^https?:\/\/([a-z0-9\-@]+\.?):?\.([a-z]{2,4}|[a-z]{2,4}:[0-9]{1,6})\/?$/i", $pURL));
    }
}

?> 
