Javascript form

0

I have a problem which the required does not work and then it does not work either go to the hello page.html

	function confirmar(){
		
		var listasexo = document.formconf.elements["genero"];
		var sexo;
	    for(var i = 0 ; i < listasexo.length; i++){
			if(listasexo[i].checked){
				sexo = listasexo[i].value;
			}
		}
		 
		var hermanos =  document.getElementById("Idhermano").value;
		 
		var dni = document.getElementById("Idtxtdni").value; 
		var nombre = document.getElementById("Idtxtname").value;
		var apellido = document.getElementById("Idtxtapellido").value;
		var direccion = document.getElementById("Idtxtdireccion").value;
		var edad = document.getElementById("Idtxtedad").value;
		var fecha = document.getElementById("Idtxtfecha").value;
		var mvl = document.getElementById("Idtxtmvl").value;
		
	    var confi =  confirm("Enviando " + dni + "Con nombre: "+ nombre + " ,Apellidos: " +apellido + " ,con "+ hermanos + " ,Sexo: " + sexo +  " ,Dirección: " + direccion +  " ,edad: " +edad + " ,nacido: " + fecha +  " , teléfono: " + mvl);
		
		if(confi){
			alert("Se envía");
		}else{
			alert("No se envía");
		}

		var contador = 0;
		var txt='';
		
		while (contador != hermanos){
			contador++;
			txt += "<label for='hermano'>Hermano "  + contador + "Nombre: <input type='text' name='txtnombre"+contador+"' value='' required></label><br/>";
			txt += "<label for='hermano'>Hermano "  + contador + "Apellido: <input type='text' name='txtapellido"+contador+"' value='' required></label><br/>";
		}
		
		document.getElementById("nuevocontenido").innerHTML += txt;
		alert("final");
		return confi;  
	}

	function comprobar_dni(obj){
		var numero;
		var letr;
	    var letra;
        var expresion_regular_dni;
 
        expresion_regular_dni = /^\d{8}[a-zA-Z]$/;
 
        if(expresion_regular_dni.test (obj.value) == true){
            numero = obj.value.substr(0,obj.value.length-1);
            letr = obj.value.substr(obj.value.length-1,1);
            numero = numero % 23;
            letra='TRWAGMYFPDXBNJZSQVHLCKET';
            letra=letra.substring(numero,numero+1);
            if (letra!=letr.toUpperCase()) {
                alert('Dni erroneo, la letra del NIF no se corresponde');
				obj.value="";
            }
        }else{
              alert('Dni erroneo, formato no válido');
			  obj.value="";
              }
    }

	 function comprobar_fecha(obj){
		alert("fecha");
	 }

     function comprobar_telf(obj){
		if(obj.value.length != 9){
			alert("La longitud del teléfono no es correcta");
			obj.value="";
		 }
	 }
<!Doctype html>
<html>
<head>
 <title>Formulario</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
</head>
<body>
	<main>
	 
		<form name="formconf" action="hola.html" method="POST">
			<label for="nombre">DNI: <input type="text" name="txtdni" id="Idtxtdni" value=""  onblur="comprobar_dni(this)" required></label><br/>
			<label for="nombre">NOMBRE: <input type="text" name="txtname" id="Idtxtname" value="" required></label><br/>
			<label for="apellido">APELLIDO: <input type="text" name="txtapellido" id="Idtxtapellido" value="" required></label><br/>
			<label for="hermanos">Hermanos: <input type="number" name="txthermano" id="Idhermano" value="" required></label><br/>
			<fieldset>
			 <legend> Sexo </legend>
			  <input type="radio" name="genero" value="Hombre"  > <label for="genhom">Hombre</label>
			  <input type="radio" name="genero" value="Mujer"  > <label for="genmuj">Mujer</label>
			</fieldset>
			<label for="direccion">DIRECCIÓN: <input type="text" name="txtdireccion" id="Idtxtdireccion" value="" required></label><br/>
			<label for="edad">EDAD: <input type="number" name="txtedad" id="Idtxtedad" value=""  required></label><br/>
			<label for="fecha_nac">FECHA NAC: <input type="text" name="txtfecha" id="Idtxtfecha" value=""  onblur="comprobar_fecha(this)" required></label><br/>
			<label for="telefono">TELEFONO: <input type="text" name="txtmvl" id="Idtxtmvl" value="" onblur="comprobar_telf(this)"  required></label><br/>
			<input type="button" value="Enviar" name="btnsend" onclick="confirmar();">
		</form>
	 
	 <div id="nuevocontenido">
	 
	 </div>
	 
	</main>  
</body>
</html>
    
asked by EduBw 03.10.2017 в 22:09
source

1 answer

0

You must change the input of type button to type submit since if the form is not sent by means of a submit you will never recognize the required

<input type="submit" value="Enviar" name="btnsend" onclick="confirmar();">

Then you must place a preventDefault() in the function of confirmar like this:

function confirmar(e){

    e.preventDefault();

    /*Resto de código*/
}

I hope you serve, greetings!

    
answered by 04.10.2017 в 00:02