? ptBR.diff Index: ptBR.php =================================================================== RCS file: /repository/pear/Validate/Validate/ptBR.php,v retrieving revision 1.9 diff -u -r1.9 ptBR.php --- ptBR.php 1 Nov 2005 13:12:30 -0000 1.9 +++ ptBR.php 30 Nov 2005 19:16:43 -0000 @@ -33,7 +33,9 @@ * - Postal code CEP (Código de Endereçamento Postal) * - CPF (Cadastro de Pessoa Física) * - CNPJ (Cadastro Nacional de Pessoa Jurídica) - * + * - Region/State (Estados brasileiros) + * - Phone Number + * * @category Validate * @package Validate_ptBR * @author Silvano Girardi Jr. @@ -50,12 +52,12 @@ * format: xxxxx-xxx * * @param string $postalCode pt_BR CEP/postalCode to validate - * @param bool optional; strong checks (e.g. against a list of postcodes) (not implanted) - * @return bool true if $cep is ok, false otherwise + * @param bool optional; strong checks (e.g. against a list of postcodes) (not implanted) + * @return bool true if $cep is ok, false otherwise */ function postalCode($postalCode, $strong = false) - { - return (bool)ereg('(^[0-9]{5})-([0-9]{3})$', $postalCode); + { + return (bool) preg_match("/^(\d{5})-(\d{3})$/",$postalCode); } /** @@ -197,5 +199,73 @@ } } } + + /** + * Validates a "region" (i.e. state) code + * + * @param string $region 2-letter state code + * @return bool true if $region is ok, false otherwise + * @static + */ + function region($region) + { + switch (strtoupper($region)) { + case 'AC': + case 'AP': + case 'AL': + case 'AM': + case 'BA': + case 'CE': + case 'DF': + case 'ES': + case 'GO': + case 'MA': + case 'MT': + case 'MS': + case 'MG': + case 'PA': + case 'PB': + case 'PR': + case 'PE': + case 'PI': + case 'RJ': + case 'RN': + case 'RS': + case 'RO': + case 'RR': + case 'SC': + case 'SP': + case 'SE': + case 'TO': + return true; + } + return false; + } + + + /** + * Validates a brazilian (ptBR) phone number. + * Also allows the formats + * If $requiredAreaCode is true: + * (XX)-XXXX-XXXX,(XX) XXXX XXXX, (XX)-XXXX XXXX, (XX) XXXX-XXXX, + * XX-XXXX-XXXX,XX XXXX XXXX,XX-XXXX XXXX,XX XXXX-XXXX,XX XXXXXXXX,(XX)XXXXXXXX + * If $requiredAreaCode is false: XXXX-XXXX,XXXX XXXX, XXXXXXXX + * @param string $number phone to validate + * @param bool $requireAreaCode require the area code? (default: true) + * @return bool The valid or invalid phone number + */ + function phoneNumber($number, $requireAreaCode = true) + { + if (!$requireAreaCode) { + if (preg_match("/^(\d{4})[- ]?(\d{4})$/", $number)) { + return true; + } + } else { + if (preg_match("/^(\()?[1-9]{2}(?(1)\))[- ]?(\d{4})[- ]?(\d{4})$/",$number)) { + return true; + } + } + return false; + } } -?> \ No newline at end of file +?>