VALIDAR EMAIL Y STRING CON PHP


/ Published in: PHP
Save to your folder(s)

Validacion de email y string con php


Copy this code and paste it in your HTML
  1. function comprobar_string($string){
  2. //compruebo que los caracteres sean los permitidos
  3. $permitidos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_áéíóúÁÉÍÓÚ";
  4. for ($i=0; $i<strlen($string); $i++){
  5. if (strpos($permitidos, substr($string,$i,1))===false){
  6. $error = "novalido".$string;
  7. return $error;
  8. }
  9. }
  10. return $string;
  11. }
  12.  
  13. function comprobar_email($email){
  14. $mail_correcto = 0;
  15. //compruebo unas cosas primeras
  16. if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
  17. if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
  18. //miro si tiene caracter .
  19. if (substr_count($email,".")>= 1){
  20. //obtengo la terminacion del dominio
  21. $term_dom = substr(strrchr ($email, '.'),1);
  22. //compruebo que la terminación del dominio sea correcta
  23. if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){
  24. //compruebo que lo de antes del dominio sea correcto
  25. $antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1);
  26. $caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1);
  27. if ($caracter_ult != "@" && $caracter_ult != "."){
  28. $mail_correcto = 1;
  29. }
  30. }
  31. }
  32. }
  33. }
  34. if ($mail_correcto)
  35. return $email;
  36. else
  37. return "novalido".$email;
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.