Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/07/2007, 02:41
marcospuig
 
Fecha de Ingreso: febrero-2007
Mensajes: 138
Antigüedad: 18 años, 2 meses
Puntos: 0
Validar javascript

¿Se podría validar este código, pero incluyendo todo en un sólo formulario en vez de en tres?

Es que no consigo hacerlo

Código:
<html><head> 
<title>Ejemplo</title> 
<script type="text/javascript">  
num=0;  
function crearAmbito(obj) {  
  num++;  
             //Creamos los datos del area  
    
  fi = document.getElementById(obj.form.id);   
  contenedor = document.createElement('div');   
  contenedor.id = 'div'+num;   
  fi.appendChild(contenedor);   

  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  
    
  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  
    
    
  txt = document.createTextNode('Nombre del ámbito: ');            //Creamos el modulo      
  ele = document.createElement('input');   
  ele.type = 'text';   
  ele.size='40';  
  ele.name = 'cNombreAmbito'+num;   
  contenedor.appendChild(txt);  
  contenedor.appendChild(ele);  

  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  
    
  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  

  txt = document.createTextNode('  Horas del ámbito: ');            //Creamos las horas del modulo  
  ele = document.createElement('input');   
  ele.type = 'text';   
  ele.name = 'cHorasAmbito'+num;   
  ele.size='5';  
  contenedor.appendChild(txt);  
  contenedor.appendChild(ele);   
  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  
  txt = document.createElement('<br>');                // Meto un salto de línea  
  contenedor.appendChild(txt);  
      
  ele = document.createElement('input');   
  ele.type = 'button';   
  ele.value = 'Borrar';   
  ele.name = 'div'+num;   
  ele.onclick = function () {borrar(this.name,obj.form.id)}   
  contenedor.appendChild(ele);   
   
}  
function borrar(obj,idfrm) {  
  fi = document.getElementById(idfrm);   
  fi.removeChild(document.getElementById(obj));   
}  
function validar(frm) { 
    var iEls=frm.elements.length; 
    for (var i=0; i<iEls; i++) { 
        var o=frm.elements[i]; 
        if (o.name.substring(1,2)=="H") { 
            alert("Valido las horas "+o.name); 
        } 
        else if (o.name.substring(1,2)=="N") { 
            alert("Valido el nombre "+o.name); 
        } 
    } 
} 
</script>  
</head> 
<body> 
<fieldset>
<form onsubmit="return false" id="fiel"> 
<table border="1" width="100%">
    <tr>
        <th><input type="button" value="Crear en este fieldset" name="B3" onclick="crearAmbito(this)"></th>
        <th><input type="button" value="Validar este fieldset" name="B3" onclick="validar(this.form)"></th>
    </tr>
</table>
</form> 
</fieldset>
<fieldset>
<form onsubmit="return false" id="fie2"> 
<table border="1" width="100%">
    <tr>
        <th><input type="button" value="Crear en este fieldset" name="B3" onclick="crearAmbito(this)"></th>
        <th><input type="button" value="Validar este fieldset" name="B3" onclick="validar(this.form)"></th>
    </tr>
</table>
</form> 
</fieldset>
<fieldset>
<form onsubmit="return false" id="fie3"> 
<table border="1" width="100%">
    <tr>
        <th><input type="button" value="Crear en este fieldset" name="B3" onclick="crearAmbito(this)"></th>
        <th><input type="button" value="Validar este fieldset" name="B3" onclick="validar(this.form)"></th>
    </tr>
</table>
</form> 
</fieldset>
</body> 
</html>
Muchas gracias