Vadio 13 Denunciar post Postado Agosto 1, 2012 olá pessoal hj eu estudando sobre expressões regulares pensei em criar uma classe de validação somente utilizando regexp. é uma das primeiras classes que eu crio aceito críticas e sugestões Obrigado. modo de utilização include 'Validate.php'; class ValidateException extends Exception{} $email = "eric_freitahotmail.com"; //eric_freitas@hotmail.com $cep = "99.99-99"; // 99.999.999 $telefone = "(99-9999"; // (99) 9999-9999 $cpf = "999.999-99"; //999.999.999-99 $length = array('limit'=>'Máximo','nc'=>'5','strlen'=>strlen("string qualquer")); /* * $limit máximo|| maximo || max || máx || mínimo etc... * */ try{ $V = new Validate(); if(!$V->email($email)) throw new ValidateException('email inválido'); if(!$V->cep($cep)) throw new ValidateException('cep inválido'); if(!$V->telefone($telefone)) throw new ValidateException('telefone inválido'); if(!$V->cpf($cpf)) throw new ValidateException('cpf inválido'); if(!$V->length($length['limit'], $length['nc'], $length['strlen'])) throw new ValidateException('Quatidade de caracteres inválido'); }catch (ValidateException $e){ echo $e->getMessage()."<br />"; } classe Validate /* @Developer - Eric Rodrigo de Freitas * Descrição - Validate * Validate->email(String $email) : BOOL * Validate->date(String $date) : BOOL * Validate->cep(String $cep) : BOOL * Validate->telefone(String $telefone) : BOOL * Validate->cpf(String $cpf) : BOOL * Validate-)length(String $limit, Int $nc, Int $strlen) : BOOL * @param $limit ('minimo','maximo') * @param $nc = Int comparador * @param $strlen = Int String.Length * */ class Validate { public function email($email){ $regex = "/^[a-z0-9_\.\-]+@[a-z0-9_\.\-]*[a-z0-9_\-]+\.[a-z]{2,4}$/i"; return preg_match($regex, $email); } public function cep($cep){ $regex = "/^[0-9]{2}.[0-9]{3}-[0-9]{3}$/"; return preg_match($regex, $cep); } public function telefone($telefone){ $regex = "/^\([0-9]{2}\) [0-9]{4}-[0-9]{4}$/"; return preg_match($regex, $telefone); } public function cpf($cpf){ $regex = "/^[0-9]{2} [0-9]{4}-[0-9]{4}$/"; return preg_match($regex, $cpf); } public function length($limit,$nc,$strlen){ $min_regex = "/^(min|mín)/i"; $max_regex = "/^(max|máx)/i"; $op = null; if(preg_match($min_regex, $limit)) $op = ">="; if(preg_match($max_regex, $limit)) $op = "<="; $r = eval("if($strlen $op $nc) return true; else return false;"); return $r; } } Compartilhar este post Link para o post Compartilhar em outros sites
Vadio 13 Denunciar post Postado Agosto 1, 2012 correção método cpf public function cpf($cpf){ $regex = "/^[0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}$/"; return preg_match($regex, $cpf); } Compartilhar este post Link para o post Compartilhar em outros sites
Andrey Knupp Vital 136 Denunciar post Postado Agosto 2, 2012 Olá, Vadio, primeiramente obrigado por compartilhar o código. Bom, sua validação de CPF está incorreta, ao menos que você queira validar somente o 'formato'. Outra coisa, os telefones você espera até 2 dígitos no DDD correto ? mas existem DDD's de três dígitos (isso se: você não estiver se limitando a DDD's Brasileiros), e se não me engano, aqui no Brasil irá acontecer uma mudança nos números de telefones, será adicionado o nono dígito, dá uma verificada pra já se adequar ao novo padrão. Abraços, Andrey Knupp Vital Compartilhar este post Link para o post Compartilhar em outros sites
Beraldo 864 Denunciar post Postado Agosto 2, 2012 você está usando apenas o ponto (.) onde deveria ser barra e ponto (\.). Tome cuidado com isso, pois o ponto equivale a um caractere qualquer, não apenas o caractere ponto Compartilhar este post Link para o post Compartilhar em outros sites