Ver Mensaje Individual
  #1 (permalink)  
Antiguo 25/07/2011, 16:29
guidodipaolo
 
Fecha de Ingreso: julio-2011
Mensajes: 15
Antigüedad: 12 años, 10 meses
Puntos: 0
Pregunta Problema para validar formulario antes de enviar datos.

Hola a todos. Este es mi primer post de ayuda de los que aseguro verán miles. Acabo de descubrir el mundo de programación web y me encantó.
Pero, llegó mi primer problema. Tengo un simple formulario el cual esta válidado con Javascript.

Código HTML:

<script>

function valida()

{ 

	if(document.form.nombre.value.length ==0) 

	{

		alert ("Ingrese un Nombre"); 

		document.form.nombre.focus();

		return false;

	} 

	

	if(isNaN(document.form.nombre.value)) 

	{ 

 	} 

  	else { 

	alert ("Ingrese un Nombre correcto"); 

	document.form.nombre.value=""; 

	document.form.nombre.focus();

	return false;

	} 

 	

	if(document.form.apellido.value.length ==0) 

	{

		alert ("Ingrese un Apellido");

		document.form.apellido.focus();

		return false;

	}  

	if(isNaN(document.form.apellido.value)) 

	{ 

 	} 

  	else { 

	alert ("Ingrese un Apellido correcto"); 

	document.form.apellido.value=""; 

	document.form.apellido.focus();

	return false;

	} 

	if(document.form.tel.value.length ==0) 

	{

		alert ("Ingrese un Teléfono"); 

		document.form.tel.focus();

		return false;

	}  

	if(isNaN(document.form.tel.value)) 

	{ 

	alert ("Ingrese un Teléfono correcto"); 

	document.form.tel.value=""; 

	document.form.tel.focus();

	return false;

	}

	if(document.form.comentario.value.length ==0) 

	{

		alert ("Ingrese un Comentario"); 

		document.form.comentario.focus();

		return false;

	}  

	var filter=/^[A-Za-z_.][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

	if (filter.test(document.form.email.value))

	{

	return true; 

	}

	else

	{

	alert("Ingrese una dirección de correo válida");

	document.form.email.focus();

	return false;

	}

}

</script>  

</head>

<body>



	<form name="form" action="enviacorreo.php" method="post" onsubmit="return valida(this)"> 

    	<table> 

			<tr>

        		<th colspan="2"> Contacto </th>

            </tr> 

            <tr>

        		<td> Nombre </td>

            	<td> <input  size="15" maxlength="15" type="text"  id="nombre" name="nombre"/> </td>    

			</tr> 

            <tr>

        		<td> Apellido </td>

            	<td> <input  size="15" maxlength="15" type="text" id="apellido" name="apellido" /> </td>    

			</tr> 

             <tr>

        		<td> E-mail </td>

            	<td> <input  size="15" maxlength="25" type="text" id="email" name="email" /> </td>    

			</tr>   

             <tr>

        		<td> Teléfono </td>

            	<td> <input  size="15" maxlength="15" type="text" id="tel" name="tel" /> </td>    

			</tr> 

             <tr>

        		<td> Comentarios </td>

            	<td> 	

                	<textarea name="comentario" cols="45" tows="5"></textarea>

                </td>    

			</tr>  

            <tr>

        		<td colspan="2" align="center">	

                	<input type="button" value="Confirma" id="boton" name="boton" onclick="valida(), document.form.submit();" />

                </td>    

			</tr> 

    </table>

	</form> 

</body> 
Y quiero que antes de hacer el submit, verifique los datos y si los datos no son correctos no se ejecute el submit
Sé que existe un atributo que es onsubmit pero no he logrado hacer que funcione.

El codigo de enviacorreo.php es el siguiente:

Código PHP:

<?  

//Recepción de datos 
$nombre=$_POST['nombre']; 
$apellido$_POST['apellido']; 
$tel=$_POST['tel']; 
$email=$_POST['email']; 
$comentario=$_POST['comentario'];  
//Fin de resepción de datos 


//Acción de EnvÃ*o 

$para='[email protected]'
$asunto='CONSULTA'
$mensaje='Mensaje recibido en www.xxxx.com.ar Datos: 

Nombre:'
.$nombre.
Apellido: '
.apellido.'
Teléfono: '
.$tel.'
E-mail: '
.$email.'
Comentario: '
.$comentario.'
'

$desde='From:  xxxx <[email protected]>';

mail($para,$asunto,$mensaje,$desde); 
echo 
'Se ha enviado correctamente el mensaje';

?>