Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/05/2012, 12:29
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Formulario PHP - Validez de los campos

Con javascript
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. /* script */
  9. function validar(){
  10.    
  11. var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  12. var email_ingresado = document.getElementById('email').value;
  13. if(!emailReg.test(email_ingresado)) {
  14. alert('email no válido');
  15. return false;
  16. }
  17. }
  18. //]]>
  19. </head>
  20. <form action="x.html" method="post" onsubmit="return validar();">
  21. <input type="text" id="email" name="email" />
  22. <input type="submit" value="enviar" />
  23. </form>
  24. </body>
  25. </html>


Con php

Código PHP:
Ver original
  1. $el_email = $_POST['email'];
  2.  
  3. $control_mail="/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$/i"; //regExp
  4. if(!preg_match($control_mail,$el_email)){
  5. $mensaje_error .= "La <b>sintáxis de email</b> no es válida<br />\n";
  6. exit();
  7. }else{
  8. // ejecuto la función mail()
  9. }

Con javascript es opcional (simplemente para mejorar la usabilidad por parte del usuario), la que vale es la hecha con php

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.