Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/12/2008, 12:36
ryder
 
Fecha de Ingreso: diciembre-2008
Mensajes: 13
Antigüedad: 16 años, 3 meses
Puntos: 0
Validar Datos desde Libreria

Hola a todos este es mi primer mensaje...hace poco empece con javascript, estoy probando un formulario pero tengo problemas para validar datos..no puedo hacer q valide el campo dni, o sea solo lo hace por php pero no con javascript nose si estará bien el validar.js o si lo estoy llamando mal pero bueno nada aca dejo el código para ver si me pueden ayudar..
son tres paginas: el 1 y el 2 son librerias..el 3 es una pagina autoprocesada..bueno esa es la idea...

validar.js

Código:
<!-- Comienza validar.js

// Mensaje que indica los campos incorrectos
var errores = "";
// Validar el campo dni 
var DNI = document.formulario.dni.value;

function ValidarDNI (DNI)
{
var Longitud_Max = 8; 
var Limite_Min = 0;
var Limite_Max = 40000000;
if (DNI.length<Longitud_Max || DNI == "" || isNaN(DNI)||DNI<Limite_Min||DNI>Limite_Max )
	{return false;}		
return true;
}
		
 
function mensaje_errores(m){
campo=0;
m = m+"__________________________________\n";
m = m+"Te faltan introducir los siguientes datos:\n"; 

if (!ValidarDNI (DNI))
{m = m+"\n   -  Numero De Documento (Solo numeros de 8 digitos)"; campo=1;} 

m=m+"\n__________________________________\n"; 
m=m+"\n¡Por favor pulsá enter, rellená los datos y probá de nuevo a ver que onda!";

if (campo==0){alert("Todo Bien") }
  else {alert(m) }
 }

mensaje_errores(errores);

//  Fin -->
FuncionFormularios.req.php

Código PHP:
<script language='javascript' src="validar.js"></script> 
<?php
$msj 
= array();
{
array_push($msj,"idFormBusqueda",     // 0                 
                 
"formulario",         // 1
                 
"$PHP_SELF",          // 2                 
                 
"NOTA: los datos marcados con (*) deben ser llenados obligatoriamente"// 3       
                 
"Formulario De Busqueda"// 4
                 
"Para dar de baja debe ingresar el DNI de usuario"// 5
                 
"Para modificar datos debe ingresar el DNI de usuario" // 6
                   
);}

function 
muestro_formulario($msj)
{
?>
<form action="<?php echo $msj[2];?>" name="<?php echo $msj[1];?>" id="<?php echo $msj[0];?>"
 method="post" onsubmit="return validar(this);">
  <table width="687">
    <tr>
      <td colspan="2"><div align="center"><?php echo $msj[4];?></div></td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2"><?php echo $msj[5];?></td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td width="155">* DNI:</td>
      <td width="520"><label for="dni"></label>
      <input name="DNI" type="text" id="dni" size="8" maxlength="8" /> 
      (Ingrese numeros)</td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2"><?php echo $msj[3];?></td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="2"><label for="enviar"></label>
      <input type="submit" name="Boton" id="Boton" value="Buscar" />
      <label for="limpiar"></label>
      <input type="reset" name="Boton" id="Boton" value="Restablecer" /></td>
    </tr>
  </table>
</form>
<?php 
// Termina Formulario 

// Validar DNI

function comprobar_dni($valor,$lim1,$lim2,$longitud){ 
  
      
//Compruebo si es un valor numérico y si la longitud es igual a /  
            
if (is_numeric('0'.$valor) and strlen($valor)==$longitud)
       {if (
$valor>$lim1 and $valor<=$lim2)
           {return 
true;} 
       }      
}

function 
muestro_mensaje_errores($errores){
echo
"<h3>-------------------------------------- Aviso ---------------------------------------</h3>";
echo
"<P>No se ha podido enviar el formulario debido a los siguientes errores:</P>\n";
echo
"<UL>\n";  
foreach(
$errores as $indice => $error)
{echo 
$error;}
echo
"</UL>\n"
echo
"------------------------------------------------------------------------------------------\n";
echo
"<br>Pulse 'Volver' para ingresar los datos, caso contrario pulse 'Cancelar'</br>";
echo
"<P>[<A HREF='javascript:history.back()'>Volver</A>][<A HREF='PruebaFormularios.php'>Cancelar</A>]</P>\n";}        

function 
muestro_datos_recibidos($registro){
echo
"<h3>------------------------------- Datos recibidos -----------------------------</h3>";
echo 
"DNI: ".$registro[0];}

?>

PruebaFormularios.php

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Prueba Formularios</title>
</head>
<body>
<?php
require_once("FuncionFormularios.req.php");
if (isset(
$_POST['Boton']) && $_POST['Boton']=="Buscar"){

// En el registro se guardan los datos recibidos desde el formulario
$registro = array();
foreach(
$_POST as $campo =>  $valor)
{
array_push($registro,$valor);}

// En el arreglo se guardan los errores da datos recibidos desde el formulario
$errores = array();

// Compruebo Datos
$lim1=0;
$lim2=40000000
$longitud=8;
if (!
comprobar_dni($registro[0],$lim1,$lim2,$longitud)){
$errores[] = " <LI>DNI de la persona (solo numeros, 8 digitos)\n"; }

// Si el vector errores no esta vacio imprime la lista de errores sino muestra los datos recibidos
if (!empty($errores))
         {
muestro_mensaje_errores($errores);}        
    else {
muestro_datos_recibidos($registro);}
      
  }
 else {
muestro_formulario($msj);}    

?>
</body>
</html>