Foros del Web » Programando para Internet » Javascript »

Validación de formulario

Estas en el tema de Validación de formulario en el foro de Javascript en Foros del Web. Tengo el siguiente formulario: Código HTML: <form id= "contact" onsubmit= "return validation();" > <label id= "label_mail" for= "mail" > Correo electr &oacute; nico: </label> <input ...
  #1 (permalink)  
Antiguo 22/05/2012, 10:08
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 12 años
Puntos: 0
Validación de formulario

Tengo el siguiente formulario:

Código HTML:
<form id="contact" onsubmit="return validation();">
	<label id="label_mail" for="mail">Correo electr&oacute;nico: </label><input id="mail" size="40em" /><br>
	<input type="radio" name="type" value="Duda" /><label>Duda</label>
	<input type="radio" name="type" value="Queja" /><label>Queja</label>
	<input type="radio" name="type" value="Sugerencia" /><label>Sugerencia</label><br>
	<label id="label_textarea" for="textarea">Escribe lo que desees: </label><br>
	<textarea cols="100%"></textarea><br>
	<button type="submit">Enviar</button>
	<button type="reset">Borrar</button>
</form> 
Y el código javascript:
Código:
function validation() {
				emailValidation();
				optionChecked();
			}
			
			function emailValidation() {
				var email = document.getElementById("mail").value;
				var patronEmail = /^(.+)@(.+)$/;
				if(patronEmail.test(email) == false) {
					var div_error = document.getElementById("div_error");
					div_error.innerHTML = "El correo electr&oacute;nico introducido es incorrecto";
					return false;
				}
			}
			
			function optionChecked() {
				var radio = document.getElementsByName("type");
 	
				var selected = false;
				for(i=0; i<radio.length; i++) {	
				  if(radio[i].checked) {
				    selected = true;
				    break;
				  }
				}

				if(selected == null) {
					var div_error = document.getElementById("div_error");
					div_error.innerHTML = "No ha sido seleccionada el tipo de consulta";
					return false;
				}
			}
No se que falla, pero nunca comprueba si el radio está marcado. ¿Qué puede ser?

Etiquetas: formulario, html, input, validar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:28.