Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/04/2009, 16:13
Avatar de hoberwilly
hoberwilly
 
Fecha de Ingreso: julio-2008
Ubicación: Lima - Perú
Mensajes: 769
Antigüedad: 15 años, 10 meses
Puntos: 2
Pregunta Ayuda...validaciones con 2 eventos onkeypress

Hola amigos, muy buenas ...para pedirles por favor apoyarme con esto:
1. Tengo mi formulario con cajas de texto el cual valido la tabulacion (pasar cada caja con enter). Ahora quisiera implementarlo la validacion de k la primera caja no permita ingresar numeros y la segunda caja no permita ingresar letras...tonces lo implemente de esta manera:
Código php:
Ver original
  1. <table>
  2. <form name="form" id="form" action="index.php?p=8" method="post" onsubmit="return envia()">
  3. <tr bgcolor="#48A4FF" align="left" class="tit">
  4.     <td height="30" colspan=2>Datos del Destinatario</td>
  5. </tr>
  6. <tr>
  7.     <td width="35%">Apellidos y Nombres: </td>
  8.     <td width="65%" align="right"><input type="text" size="55" maxlength="50" name="destinatario" class="input_text" onKeypress="tabular(event,this),validaLet(event)">&nbsp<font color="red">(*)</td>
  9. </tr>
  10. <tr>
  11.     <td width="35%">Direcci&oacute;n de envio:</td>
  12.     <td width="65%" align="right"><input type="text" size="55" maxlength="50" name="direccion" onKeypress="tabular(event,this);validaNum(event)" class="input_text">&nbsp<font color="red">(*)</td>
  13. </tr>
  14. <tr align="center" valign="middle">
  15.      <td width="35%"></td>
  16.      <td width="15%"><input type="button" value="Anterior" onclick="history.go(-1)"></td>
  17.      <td width="15%"><input type="submit" name="Confirmar" value="Confirmar"></td>
  18.      <td width="35%"></td>
  19. </tr>
  20. </table>
  21. </form>
Aki el codigo js:
Código php:
Ver original
  1. function validaLet(e){
  2.     tecla = (document.all) ? e.keyCode : e.which;
  3.     if (tecla==8) return true; //Tecla de retroceso (para poder borrar)
  4.     patron = /\d/; // Solo números
  5.     te = String.fromCharCode(tecla);
  6.     if(patron.test(te)){
  7.         alert('Acepta solo letras');
  8.         return false;
  9.     }
  10. //  return patron.test(e);
  11. }
  12. function validaNum(e){
  13.     tecla = (document.all) ? e.keyCode : e.which;
  14.     if (tecla==8) return true;
  15.     patron =/[A-Za-zñÑ\s]/; // Solo letras, incluye ñ y Ñ, ademas barra espaciadora
  16.     te = String.fromCharCode(tecla);
  17.     if(patron.test(te)){
  18.         alert('Acepta solo numeros');
  19.         return false;
  20.     }
  21. }
  22. function validaVacio(){
  23.     if(document.form.destinatario.value==""){
  24.         alert("Ingrese Apelidos y Nombres");
  25.         form.destinatario.focus();
  26.         return false;
  27.     }
  28.     if(document.form.direccion.value==""){
  29.         alert("Ingrese Direccion");
  30.         form.direccion.focus();
  31.         return false;
  32.     }
  33.     return true;
  34. }
  35. function confirma(){
  36.     if(confirm('¿Desea imprimir el pedido?'))
  37.         print();
  38.     if(confirm('¿Esta seguro de confirmar su pedido?'))
  39.         return true;
  40.         return false;
  41. }
  42. function envia(){
  43.     if(validaVacio()){
  44.         return confirma();
  45.     }
  46.     return false;
  47. }
Pero lo unico k me falla es k al dar tabulacion a una caja vacia me da alerta de validacion de la funcion validaVacio (del evento onsubmit)??? ojo k no estoy dando click en el boton submit...estoy dando enter en las cajas de texto.

Gracias de antemano,