Ver Mensaje Individual
  #3 (permalink)  
Antiguo 26/11/2015, 16:09
pilucho
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 655
Antigüedad: 19 años, 5 meses
Puntos: 6
Respuesta: Validar un formulario

Aqui tienes un ejemplo personalizas los nombres si gustas

funciones.js

Código PHP:
Ver original
  1. <script>
  2. $(function() {
  3.     var emailreg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
  4.     $(".boton").click(function(){  
  5.         $(".error").fadeOut().remove();
  6.        
  7.         if ($(".nombre").val() == "") {  
  8.             $(".nombre").focus().after('<span class="error">Ingrese su nombre</span>');  
  9.             return false;  
  10.         }  
  11.         if ($(".email").val() == "" || !emailreg.test($(".email").val())) {
  12.             $(".email").focus().after('<span class="error">Ingrese un email correcto</span>');  
  13.             return false;  
  14.         }  
  15.         if ($(".asunto").val() == "") {  
  16.             $(".asunto").focus().after('<span class="error">Ingrese un asunto</span>');  
  17.             return false;  
  18.         }  
  19.         if ($(".mensaje").val() == "") {  
  20.             $(".mensaje").focus().after('<span class="error">Ingrese un mensaje</span>');  
  21.             return false;  
  22.         }  
  23.     });  
  24.     $(".nombre, .asunto, .mensaje").bind('blur keyup', function(){  
  25.         if ($(this).val() != "") {             
  26.             $('.error').fadeOut();
  27.             return false;  
  28.         }  
  29.     });
  30.     $(".email").bind('blur keyup', function(){  
  31.         if ($(".email").val() != "" && emailreg.test($(".email").val())) { 
  32.             $('.error').fadeOut();  
  33.             return false;  
  34.         }  
  35.     });
  36. });
  37. </script>

Formulario
Código PHP:
Ver original
  1. <?php
  2.     if(isset($_POST['boton'])){
  3.         if($_POST['nombre'] == ''){
  4.             $errors[1] = '<span class="error">Ingrese su nombre</span>';
  5.         }else if($_POST['email'] == '' or !preg_match("/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/",$_POST['email'])){
  6.             $errors[2] = '<span class="error">Ingrese un email correcto</span>';
  7.         }else if($_POST['asunto'] == ''){
  8.             $errors[3] = '<span class="error">Ingrese un asunto</span>';
  9.         }else if($_POST['mensaje'] == ''){
  10.             $errors[4] = '<span class="error">Ingrese un mensaje</span>';
  11.         }else{
  12.             $dest = "[email protected]"; //Email de destino
  13.             $nombre = $_POST['nombre'];
  14.             $email = $_POST['email'];
  15.             $asunto = $_POST['asunto']; //Asunto
  16.             $cuerpo = $_POST['mensaje']; //Cuerpo del mensaje
  17.             //Cabeceras del correo
  18.             $headers = "From: $nombre <$email>\r\n"; //Quien envia?
  19.             $headers .= "X-Mailer: PHP5\n";
  20.             $headers .= 'MIME-Version: 1.0' . "\n";
  21.             $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //
  22.  
  23.             if(mail($dest,$asunto,$cuerpo,$headers)){
  24.                 $result = '<div class="result_ok">Email enviado correctamente </div>';
  25.                 // si el envio fue exitoso reseteamos lo que el usuario escribio:
  26.                 $_POST['nombre'] = '';
  27.                 $_POST['email'] = '';
  28.                 $_POST['asunto'] = '';
  29.                 $_POST['mensaje'] = '';
  30.             }else{
  31.                 $result = '<div class="result_fail">Hubo un error al enviar el mensaje </div>';
  32.             }
  33.         }
  34.     }
  35. ?>
  36. <html>
  37.     <head>
  38.         <title>Contacto</title>
  39.         <link rel='stylesheet' href='estilos.css'>
  40.         <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
  41.         <script src='funciones.js'></script>
  42.     </head>
  43.     <body>
  44.         <form class='contacto' method='POST' action=''>
  45.             <div><label>Tu Nombre:</label><input type='text' class='nombre' name='nombre' value='<?php if(isset($_POST['nombre'])){ echo $_POST['nombre']; } ?>'><?php if(isset($errors)){ echo $errors[1]; } ?></div>
  46.             <div><label>Tu Email:</label><input type='text' class='email' name='email' value='<?php if(isset($_POST['email'])){ $_POST['email']; } ?>'><?php if(isset($errors)){ echo $errors[2]; } ?></div>
  47.             <div><label>Asunto:</label><input type='text' class='asunto' name='asunto' value='<?php if(isset($_POST['asunto'])){ $_POST['asunto']; } ?>'><?php if(isset($errors)){ echo $errors[3]; } ?></div>
  48.             <div><label>Mensaje:</label><textarea rows='6' class='mensaje' name='mensaje'><?php if(isset($_POST['mensaje'])){ $_POST['mensaje']; } ?></textarea><?php if(isset($errors)){ echo $errors[4]; } ?></div>
  49.             <div><input type='submit' value='Envia Mensaje' class='boton' name='boton'></div>
  50.             <?php if(isset($result)) { echo $result; } ?>
  51.         </form>
  52.     </body>
  53. </html>