Ver Mensaje Individual
  #6 (permalink)  
Antiguo 25/05/2012, 02:18
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 17 años, 8 meses
Puntos: 1567
Respuesta: Borrar datos de Form

Con keyup funciona bien, además cuando haces ese tipo de validaciones contra el uso del teclado, hay que evitar los alert(), porque suelen generar problemas.
Lo más apropiado sería algo asi

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. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  5. <title>validar entero</title>
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. function validar(campo){
  9. var elcampo = document.getElementById(campo);  
  10. if((!validarEntero(elcampo.value))||(elcampo.value == "")){
  11. elcampo.value = "";
  12. elcampo.focus();
  13. //alert('Debe ingresar un número');
  14. document.getElementById('mensaje').innerHTML = 'Debe ingresar un número';
  15. }else{
  16. document.getElementById('mensaje').innerHTML = '';
  17. }
  18. }
  19.  
  20. function validarEntero(input){
  21. return !isNaN(input)&&parseInt(input)==input;
  22. }
  23. //]]>
  24. </head>
  25. <form action="#">
  26. <input type="text" id="pregunta" name="pregunta" value="" onkeyup="validar(this.id);" /><br />
  27. <input type="text" id="pregunta2" name="pregunta2" value="" onkeyup="validar(this.id);" /><br /><br />
  28. <input type="submit" value="procesar" /><br /><br />
  29. </form>
  30. <div id="mensaje"></div>
  31. </body>
  32. </html>

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