Ver Mensaje Individual
  #5 (permalink)  
Antiguo 01/12/2012, 16:47
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: Funcion parseInt

vallidación de un número entero, ejemplo

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)){
  11. elcampo.value = "";
  12. elcampo.focus();
  13. alert('Debe ingresar un número entero');
  14. }
  15. }
  16.  
  17. function validarEntero(input){
  18. return (!isNaN(input)&&parseInt(input)==input);
  19. }
  20. //]]>
  21. </head>
  22. <form action="#">
  23. <input type="text" id="entero" name="entero" value="" onkeyup="validar(this.id);" />
  24. <input type="submit" value="procesar" />
  25. </form>
  26. </body>
  27. </html>

podés cambiar por un evento onsubmit en el form

En caso de querer validar un número, entero o flotante, usas parseFloat en la función validarEntero()

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