Validation class
Posted: Wed Jul 13, 2011 9:46 am
A simple validation class.jacek wrote:This is an archive post, any discussion should take place here http://betterphp.co.uk/board/viewtopic.php?f=5&t=492
class Validation { const EMAIL_PATTERN = '#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#'; const FR_PHONE_PATTERN = '#^0[1-68]([. -]?[0-9]{2}){4}$#'; const URL_PATTERN = '#^(http(|s)|ftp)://([0-9a-z.-_]+:[0-9a-z.-_]+@)?[0-9a-z.-]+\.[a-z]{2,4}(:[0-9]{2,6})?(/.+)?#i'; const DATE_PATTERN = '#^((0?[1-9])|([1-2][0-9])|(3[01]))[/\\.-_ ]((0?[1-9])|(1[0-2]))[/\\.-_ ]([0-9]{4})$#'; const ALPHA_PATTERN = '#^[a-z]+$#i'; const ALNUM_PATTERN = '#^[a-z0-9]+$#i'; public static function email($email) { return (preg_match(self::EMAIL_PATTERN, $email)) ? TRUE : FALSE; } public static function frenchPhone($number) { return (preg_match(self::FR_PHONE_PATTERN, $number)) ? TRUE : FALSE; } public static function url($url) { return (preg_match(self::URL_PATTERN, $url)) ? TRUE : FALSE; } public static function date($date) { return (preg_match(self::DATE_PATTERN, $date)) ? TRUE : FALSE; } public static function alpha($string) { return (preg_match(self::ALPHA_PATTERN, $string)) ? TRUE : FALSE; } public static function alnum($string) { return (preg_match(self::ALNUM_PATTERN, $string)) ? TRUE : FALSE; } /* Number is between * * @created : 9.7.2011 * @function : find out if a number is between two other number * @limitations : numberBetween(5, 25, 24.999999999999999) will return FALSE even though it should be true */ public static function numberBetween($low, $high, $number) { return ((float)$number > (float)$low && (float)$number < (float)$high) ? TRUE : FALSE; } }