Foros del Web » Programando para Internet » Javascript »

Validar Email y Telefono

Estas en el tema de Validar Email y Telefono en el foro de Javascript en Foros del Web. HOLA A TODOS LO QUE PASA ES QUE TENGO EL SIGUIENTE CODIGO QUE QUISIERA QUE ME AYUDARAN A AGREGAR DOS FUNCIONES MAS PERO NO ME ...
  #1 (permalink)  
Antiguo 10/10/2009, 21:48
 
Fecha de Ingreso: mayo-2009
Ubicación: Stgo. Pinotepa Nacional Oaxaca
Mensajes: 34
Antigüedad: 15 años
Puntos: 0
Validar Email y Telefono

HOLA A TODOS LO QUE PASA ES QUE TENGO EL SIGUIENTE CODIGO QUE QUISIERA QUE ME AYUDARAN A AGREGAR DOS FUNCIONES MAS PERO NO ME FUNCIONAN:::
Código TEXTO:
Ver original
  1. <script type="text/javascript">  
  2.                 validateForm=function()
  3.                 {
  4.                     var txtNombre = new Array(document.getElementById('txtNombre'), document.frmContactanos['txtNombre'].value);
  5.                     var txtDireccion = new Array(document.getElementById('txtDireccion'), document.frmContactanos['txtDireccion'].value);
  6.                     var txtEmail = new Array(document.getElementById('txtEmail'), document.frmContactanos['txtEmail'].value);
  7.                     var txtTelefono = new Array(document.getElementById('txtTelefono'), document.frmContactanos['txtTelefono'].value);
  8.                     var txtPoblacion = new Array(document.getElementById('txtPoblacion'), document.frmContactanos['txtPoblacion'].value);
  9.                     var txtAsunto = new Array(document.getElementById('txtAsunto'), document.frmContactanos['txtAsunto'].value);
  10.                     var txtComentario = new Array(document.getElementById('txtComentario'), document.frmContactanos['txtComentario'].value);
  11.                     var submit = document.getElementById('btnEnviar'); 
  12.  
  13.                     /* Eventos */
  14.                     txtNombre[0].onfocus = onFocus; txtNombre[0].onblur = onBlur;
  15.                     txtDireccion[0].onfocus = onFocus; txtDireccion[0].onblur = onBlur;
  16.                     txtEmail[0].onfocus = onFocus; txtEmail[0].onblur = onBlur;
  17.                     txtTelefono[0].onfocus = onFocus; txtTelefono[0].onblur = onBlur;
  18.                     txtPoblacion[0].onfocus = onFocus; txtPoblacion[0].onblur = onBlur;
  19.                     txtAsunto[0].onfocus = onFocus; txtAsunto[0].onblur = onBlur;
  20.                     txtComentario[0].onfocus = onFocus; txtComentario[0].onblur = onBlur;
  21.                     submit.onclick = validateFields;
  22.  
  23.                     /* Funciones */
  24.                     function onFocus() { if(document.frmContactanos[this.name].value==eval(this.name)[1]) document.frmContactanos[this.name].value='' }
  25.  
  26.                     function onBlur() {
  27.                         var value = document.frmContactanos[this.name].value;
  28.                         value = value.replace(/^\s*/, ''); value = value.replace(/\s*$/, '');
  29.                         if(!value) document.frmContactanos[this.name].value = eval(this.name)[1]
  30.                     }
  31.  
  32.                     function validateFields() {
  33.                         if(document.frmContactanos['txtNombre'].value == txtNombre[1]) { document.frmContactanos['txtNombre'].focus(); alert("El campo 'Nombre' es obligatorio."); }
  34.                         else if(document.frmContactanos['txtDireccion'].value == txtDireccion[1]) { document.frmContactanos['txtDireccion'].focus(); alert('El campo "Dirección" es obligatorio.'); }
  35.                         else if(document.frmContactanos['txtEmail'].value == txtEmail[1]) { document.frmContactanos['txtEmail'].focus(); alert('El campo "Email" es obligatorio.'); }
  36.                         else if(document.frmContactanos['txtTelefono'].value == txtTelefono[1]) { document.frmContactanos['txtTelefono'].focus(); alert('El campo "Teléfono" es obligatorio.'); }
  37.                         else if(document.frmContactanos['txtPoblacion'].value == txtPoblacion[1]) { document.frmContactanos['txtPoblacion'].focus(); alert('El campo "Población" es obligatorio.'); }
  38.                         else if(document.frmContactanos['txtAsunto'].value == txtAsunto[1]) { document.frmContactanos['txtAsunto'].focus(); alert('El campo "Asunto" es obligatorio.'); }
  39.                         else if(document.frmContactanos['txtComentario'].value == txtComentario[1]) { document.frmContactanos['txtComentario'].focus(); alert('El campo "Comentario" es obligatorio.'); }
  40.                         else { return true; }
  41.                         return false;
  42.                     }
  43.                 }
  44.  
  45.                 if(document.all&&window.attachEvent) { window.attachEvent("onload",validateForm); } // IE-Win
  46.                 else if(window.addEventListener) { window.addEventListener("load",validateForm,false); }
  47.                
  48.                 function validarEmail(valor)
  49.                 {
  50.                     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/.test(valor))
  51.                     {
  52.                         alert("La dirección de email " + valor + " es correcta.");
  53.                     }
  54.                     else
  55.                     {
  56.                         alert("La dirección de email es incorrecta.");
  57.                     }
  58.                 }
  59.                 </script>

Y EN MI FORM TENGO LO SIGUIENTE:
Cita:
<form name="frmContactanos" method="post" action="#" enctype="multipart/form-data">
<input name="txtNombre" type="text" id="txtNombre" tabindex="1" class="input-text" title="Introduce tu nombre" value="---Nombre---" accesskey="n">
<input name="txtDireccion" type="text" id="txtDireccion" tabindex="2" class="input-text" title="Introduce tu dirección" value="---Dirección---" accesskey="d">
<input name="txtTelefono" type="text" id="txtTelefono" tabindex="3" class="input-text" title="Introduce tu teléfono" value="---Teléfono---" accesskey="t">
<input name="txtEmail" type="text" id="txtEmail" tabindex="4" class="input-text" title="Introduce tu email" value="---Email---" accesskey="e" onblur="validarEmail(this.frmContactanos.txtEmail. value);">
<input name="txtPoblacion" type="text" id="txtPoblacion" tabindex="5" class="input-text" title="Introduce tu población" value="---Población---" accesskey="p">
<input name="txtAsunto" type="text" id="txtAsunto" tabindex="6" class="input-text" title="Introduce tu asunto" value="---Asunto---" accesskey="a">
<textarea name="txtComentario" cols="50" rows="4" id="txtComentario" tabindex="7" title="Introduce tus comentarios" accesskey="c">---Escribe aquí tus Comentarios---</textarea>
<input type="submit" name="btnEnviar" value="Enviar" class="green" title="Enviar">
Y NO ESTOY USANDO NINGUN EVENTO PARA EL BOTON SUBMIT ASI QUE NECESITO AGREGAR UNA FUNCION DE EMAIL PERO CUANDO LE DOY EN ENVIAR YA NO ME FUNCIONA LA FUNCION LA IGNORA:

Código:
function validarEmail(valor) 
				{
					if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/.test(valor))
					{
						alert("La dirección de email " + valor + " es correcta.");
					} 
					else 
					{
						alert("La dirección de email es incorrecta.");
					}
				}
  #2 (permalink)  
Antiguo 10/10/2009, 22:03
Avatar de loncho_rojas
Colaborador
 
Fecha de Ingreso: octubre-2008
Ubicación: En el mejor lugar del mundo
Mensajes: 2.704
Antigüedad: 15 años, 6 meses
Puntos: 175
Respuesta: Validar Email y Telefono

esta dificil.. es decir.. sin un evento de mouse que habilite las funciones, sera complicado..

puedes probar en el FORM

<form ... bl, bla, bla... onSubmit="TuFuncion(); return false;">

es decir, cuando se envia, habilita la funcion validadora... aunque veo un monton de funciones que se podrían simplificar terriblemente para no complicarte la vida.. al final, los spamers pasaran de fino, salvo que pongas un Captcha efectivo, y si solo necesitas que el usuario inserte los datos adecuados, podrias usar algo parecido a esto

Código javascript:
Ver original
  1. function ValidarEnvio()
  2.     {
  3.         var mensaje = "";
  4.         var frm = document.formContactos;
  5.        
  6.         if(frm.name.value=="")
  7.         {
  8.             mensaje += "    > Debes ingresar su Nombre y apellido.\n";
  9.         }
  10.        
  11.         if(isNaN(parseInt(frm.fono.value)))
  12.         {
  13.             mensaje += "    > Debes ingresar su Telefono o Celular. Debe contener numeros\n";
  14.         }
  15.        
  16.         if (frm.mail.value.indexOf("@")<3)
  17.         {
  18.             mensaje += "    > Debes ingresar un email valido para contactarte.\n";
  19.         }
  20.        
  21.        
  22.         if(frm.mensj.value=="")
  23.         {
  24.             mensaje += "    > Debe ingresar un Mensaje.\n";
  25.         }
  26.        
  27.         if(mensaje!="")
  28.         {
  29.             alert("No se puede enviar el mensaje debido a que: \n"+mensaje);
  30.             return false;
  31.         }else{
  32.             return true;
  33.         }
  34.     }
<form action="loquesea.php" method="post" name="formContactos" onSubmit="return ValidarEnvio();">
__________________
Ayudo con lo que puedo en el foro, y solo en el foro.. NO MENSAJES PRIVADOS.. NO EMAILS NI SKYPE u OTROS.

Antes de hacer un TOPICO piensa si puedes hallarlo en Google o en el Buscador del Foro...
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 09:54.