Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/10/2010, 12:48
oscarbt
 
Fecha de Ingreso: abril-2009
Ubicación: Colombia
Mensajes: 949
Antigüedad: 15 años, 10 meses
Puntos: 27
Validar Ingreso de campo numerico sin punto decimal

Buenas a todos, estoy elaborando un pequeño algoritmo para calcular el area de un poligono de n lados, cuando hablo de n, el poligono puede tener de 1 a 1000000 de lados, entonces lo que quiero validar que el usuario cuando me ingrese el numero de lados, solo ingrese valores enteros sin comas decimales..

Es decir que solo ingrese: 1, 2, 250, pero no 1.5, 2.6
Por ahora tengo validado que no ingrese ni valores negativos, ni caracteres, ni superiores a 1000000

Aca el codigo de mi algoritmo:

Código HTML:
Ver original
  1.  
  2. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  3. <title>Poligonos</title>
  4.  
  5. </head>
  6.  
  7. <h1><b>CALCULO DE AREA Y PERIMETRO DE UN POLIGONO </b></h1>
  8.  
  9.  
  10. <form name="datos">
  11.  
  12.  
  13.     <h3>NUMERO DE LADOS </h3>
  14.  
  15.   <input  type="text" name="lado" size="10" maxlength="7" />
  16.   <h3>VALOR DEL LADO </h3>
  17.  
  18.   <input  type="text" name="valor" size="10" />
  19.  <h3>VALOR APOTEMA </h3>
  20.  
  21.   <input  type="text" name="apotema" size="10"  /><br>
  22.    
  23.    
  24.    <h3>VALOR PERIMETRO </h3>
  25. <input type="text" name="perimetro">
  26.  
  27.   <h3>VALOR AREA </h3>
  28. <input type="text" name="resultado"><br>
  29.    <br>
  30.      
  31.  
  32.     <br>
  33.    
  34.   <input type="button" value="Calcular valores" onClick="area()"> <input name="Limpiar"  type="reset" id="Limpiar" value="Borrar valores ingresados ">
  35.  
  36. </form>
  37.  
  38. <br />
  39. </body>
  40.  
  41. </html>

Y aca el codigo JavaScript donde valido (ubicando en el head):


Código Javascript:
Ver original
  1. <script languaje="JavaScript">
  2.  function area()
  3.  {
  4.  lado=document.datos.lado.value;
  5. valor=document.datos.valor.value;
  6. apotema=document.datos.apotema.value;
  7. lado = parseFloat(datos.lado.value, 10)
  8. valor = parseFloat(datos.valor.value, 10) ;
  9. apotema = parseFloat(datos.apotema.value, 10) ;
  10.             if ((document.datos.lado.value.length== "") ||         (document.datos.valor.value.length== "") || (document.datos.apotema.value.length== ""))
  11.                
  12.                {
  13.         alert("EXISTEN VALORES NO INGRESADOS");
  14.         return false;
  15.        
  16.                }
  17.              if ((document.datos.lado.value <= 0) || (document.datos.valor.value <= 0) || (document.datos.apotema.value <= 0))
  18.                
  19.                {
  20.         alert("SOLO PUEDE INGRESAR VALORES POSITIVOS");
  21.         return false;
  22.        
  23.           }
  24.          
  25.            if ((document.datos.lado.value <= 0) ||  (document.datos.lado.value > 1000000))
  26.                
  27.                {
  28.         alert("EL NUMERO DE LADOS DEL POLIGONO ES ENTRE 1 - 1 0 0 0 0 0 0");
  29.         return false;
  30.        
  31.           }
  32.           if ( isNaN(lado) ||   isNaN(valor) || isNaN(apotema))
  33.            {  
  34.         alert("TODOS LOS CAMPOS DEBEN SER VALORES NUMERICOS");
  35.                
  36.         return false  ;
  37.  
  38.         }
  39.          
  40.          
  41.              
  42.             perimetro=lado*valor;
  43.            
  44.            
  45.             document.datos.perimetro.value=perimetro;
  46.              document.datos.resultado.value=perimetro*apotema/2;
  47.           }
  48.            
  49.      
  50.       </script>

El algoritmo funciona y hace los calculos correctamente pero.......Como puedo hacer esa validación para que el usuario no me ingrese puntos decimales??

Agradezco me puedan colaborar