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<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[
function validar(campo){
var elcampo = document.getElementById(campo);
if((!validarEntero(elcampo.value))||(elcampo.value == "")){
elcampo.value = "";
elcampo.focus();
//alert('Debe ingresar un número');
document.getElementById('mensaje').innerHTML = 'Debe ingresar un número';
}else{
document.getElementById('mensaje').innerHTML = '';
}
}
function validarEntero(input){
return !isNaN(input)&&parseInt(input)==input;
}
//]]>
<input type="text" id="pregunta" name="pregunta" value="" onkeyup="validar(this.id);" /><br /> <input type="text" id="pregunta2" name="pregunta2" value="" onkeyup="validar(this.id);" /><br /><br /> <input type="submit" value="procesar" /><br /><br />
Saludos