¿Supiste lo que acaba de lanzar LagriHost? Sí, como lo estabas esperando, ahora vendemos Web Hosting y Dominios. Conoce más :)


Calificación:
  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Chequea el correcto ingreso de , fecha , ip, url, email, dni
#1
0
Código fuente de función PHP que chequea el correcto ingreso de , fecha , ip, url, email, dni

Código fuente de función en php que permite validar una fecha, o un e-mail o una dirección IP o una URL o un DNI.

Las validaciones en PHP son tan importantes como tediosas , por eso es bueno tener a mano siempre un conjunto de funciones que nos faciliten el trabajo , estas validaciones son excelentes ya que nos permiten validar los campos que usualemente pueden contener errores de los usuarios.

Código PHP:
<?php
 
if (isset($_POST['bt']))
 { 
 if (
validar($_POST['email'],'email'))
 {print 
"email: ok <br/>";}
 else
 {print 
"email: error<br/>";}

 if (
validar($_POST['dni'],'dni'))
 {print 
"dni: ok<br/>";}
 else
 {print 
"dni: error<br/>";}
 
 if (
validar($_POST['fecha'],'fecha'))
 {print 
"fecha: ok<br/>";}
 else
 {print 
"fecha: error<br/>";}
 
 if (
validar($_POST['ip'],'ip'))
 {print 
"ip: ok<br/>";}
 else
 {print 
"ip: error<br/>";}
 
 if (
validar($_POST['url'],'url'))
 {print 
"url: ok<br/>";}
 else
 {print 
"url: error<br/>";}
 
 }
?>


<form action="t.php" method="POST" name="as">
<input type ="text" name="email"value ="soporte@lagrihost.com"/><br/>
<input type ="text" name="dni"value ="27635598"/><br/>
<input type ="text" name="fecha"value ="04/10/1979"/><br/>
<input type ="text" name="ip"value ="127.0.0.1"/><br/>
<input type ="text" name="url"value ="http://www.lagrihost.com"/><br/>
<input type ="submit" name="bt"value ="Enviar"/><br/>
</form>
 
<?php 
 
function validar($texto,$tipo_validacion)
 {
 if(
$tipo_validacion == 'email')
 {
 if(
ereg('(^[0-9a-zA-Z]+(\.[0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(\.[0-9a-zA-Z]+)*\.[0-9a-zA-Z]{2,3})$',$texto,$res))
 {return 
true;}
 }
 if(
$tipo_validacion == 'dni')
 {
 if(
ereg('^([[:digit:]]{8})$',$texto,$res))
 {return 
true;}
 }
 if(
$tipo_validacion == 'fecha'// debe incluir el 0 cuando es un solo nro , ejem : 01-12-2008
 
{
 if(
ereg('^([012][1-9]|3[01])(/|-)(0[1-9]|1[012])(/|-)([[:digit:]]{4})$',$texto,$res))
 {return 
true;}
 }
 if(
$tipo_validacion == 'ip')
 {
 if(
ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$',$texto,$res))
 {return 
true;}
 }
 if(
$tipo_validacion == 'url')// debe comenzar con   "http://"
 
{
 if(
ereg('^[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*([/?|&|=|;]?[a-zA-Z0-9_/-/%])*$',$texto,$res))
 {return 
true;}
 }
 
 
 
return false;
 }
 
?>



Posibles temas similares…
Tema Autor Respuestas Vistas Último mensaje
  LA FECHA EN CUALQUIER IDIOMA CON PHP torgar22 0 2,041 26-08-2015, 02:17 PM
Último mensaje: torgar22
  [PHP] Capturamos la URL de una página torgar22 2 2,826 04-04-2015, 05:58 PM
Último mensaje: asanbus
  Correo o Email en PHP con HTML torgar22 0 1,967 28-02-2015, 10:28 AM
Último mensaje: torgar22
  Función PHP para validar un email (incluyendo subdominios) ydcaza 0 1,961 17-02-2015, 12:58 PM
Último mensaje: ydcaza
  Mostrar fecha y hora en español con Php torgar22 3 2,740 04-02-2015, 11:17 AM
Último mensaje: asanbus



Usuarios navegando en este tema:
1 invitado(s)


Forum software by © MyBB Theme © Kevinex & iAndrew 2018