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 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;
}
?>