Skip to content

Instantly share code, notes, and snippets.

@Javlopez
Created April 27, 2011 17:46
Show Gist options
  • Save Javlopez/944773 to your computer and use it in GitHub Desktop.
Save Javlopez/944773 to your computer and use it in GitHub Desktop.
Solo numeros con 1 punto y maximo dos decimales
<script type="text/javascript">
<!--
function filterFloat(evt,input){
// Backspace = 8, Enter = 13, ‘0′ = 48, ‘9′ = 57, ‘.’ = 46, ‘-’ = 43
var key = window.Event ? evt.which : evt.keyCode;
var chark = String.fromCharCode(key);
var tempValue = input.value+chark;
if(key >= 48 && key <= 57){
if(filter(tempValue)=== false){
return false;
}else{
return true;
}
}else{
if(key == 8 || key == 13 || key == 0) {
return true;
}else if(key == 46){
if(filter(tempValue)=== false){
return false;
}else{
return true;
}
}else{
return false;
}
}
}
function filter(__val__){
var preg = /^([0-9]+\.?[0-9]{0,2})$/;
if(preg.test(__val__) === true){
return true;
}else{
return false;
}
}
-->
</script>
<input type="text" name="moneda nac" id="moneda_nac" value="10" onkeypress="return filterFloat(event,this);"/>
@MultiservIT
Copy link

Excelente. Gracias!!!!

@keie
Copy link

keie commented Feb 27, 2018

Thank you my bro!!

@RicrdoMedina
Copy link

Muchas Gracias.

@ddelacruzs
Copy link

Muchas gracias estimado, me sirvió mucho el código

@alfonsomozkoh
Copy link

Me salvaste, ya debo aprender javascript XD

@JohanRS
Copy link

JohanRS commented Feb 19, 2019

Gracias amigo, el codigo es excelente. 👍

@YorHen2719
Copy link

Seria mucha molestia explicar que hacen algunas de las lineas de codigo, soy nuevo en javascript y hay algunas cosas que no entendí, porfavor se lo agradecería :D

@legarnica
Copy link

Muchas gracias por tu trabajo, me ahorraste mucho tiempo. :)

@legarnica
Copy link

legarnica commented Aug 1, 2019

Seria mucha molestia explicar que hacen algunas de las lineas de codigo, soy nuevo en javascript y hay algunas cosas que no entendí, porfavor se lo agradecería :D

Hola, podrías indicar cuales son las líneas en las que tienes dudas y quizás entre todos te podamos ayudar 👍

@Devjs98
Copy link

Devjs98 commented Nov 12, 2019

excelente código gracias!!!

@joanpaae
Copy link

Funciona perfecto. Gracias!

@joseusa192
Copy link

muchas gracias,

@dberroteran
Copy link

dberroteran commented Apr 11, 2020

lo probe yo mismo habia omitido un framento de codigo gracias funciona excelente

@alexsuarezma
Copy link

Buenisimo!!

@elsaCapunta
Copy link

muy bueno!

@jovannaGithub
Copy link

Excelente mi gracias

@JorgeTec9206
Copy link

gracias carnal, esta perfecto

@Javlopez
Copy link
Author

Javlopez commented May 5, 2021

Hey creo que sigue siendo util, voy a dejar aquí un update de la misma.

<script type="text/javascript">
<!--
function filterFloat(evt,input){
    // Backspace = 8, Enter = 13, ‘0′ = 48, ‘9′ = 57, ‘.’ = 46, ‘-’ = 43
    var key = window.Event ? evt.which : evt.keyCode;   
    var chark = String.fromCharCode(key);
    var tempValue = input.value+chark;
    var isNumber = (key >= 48 && key <= 57);
    var isSpecial = (key == 8 || key == 13 || key == 0 ||  key == 46);
    if(isNumber || isSpecial){
        return filter(tempValue);
    }        
    
    return false;    
    
}
function filter(__val__){
    var preg = /^([0-9]+\.?[0-9]{0,2})$/; 
    return (preg.test(__val__) === true);
}
-->
</script>
<input type="text" name="moneda nac" id="moneda_nac" value="10" onkeypress="return filterFloat(event,this);"/>

Espero que siga siendo util, saludos

@arturexxx
Copy link

Gracias bro!!

@dannyolivera
Copy link

dannyolivera commented May 16, 2022

Consulta y si quiero editar el input, no me permite a no ser que elimine todo el valor en caso tenga el punto porque si es entero si permite :c

@JGuerreroS
Copy link

Hey creo que sigue siendo util, voy a dejar aquí un update de la misma.

<script type="text/javascript">
<!--
function filterFloat(evt,input){
    // Backspace = 8, Enter = 13, ‘0′ = 48, ‘9′ = 57, ‘.’ = 46, ‘-’ = 43
    var key = window.Event ? evt.which : evt.keyCode;   
    var chark = String.fromCharCode(key);
    var tempValue = input.value+chark;
    var isNumber = (key >= 48 && key <= 57);
    var isSpecial = (key == 8 || key == 13 || key == 0 ||  key == 46);
    if(isNumber || isSpecial){
        return filter(tempValue);
    }        
    
    return false;    
    
}
function filter(__val__){
    var preg = /^([0-9]+\.?[0-9]{0,2})$/; 
    return (preg.test(__val__) === true);
}
-->
</script>
<input type="text" name="moneda nac" id="moneda_nac" value="10" onkeypress="return filterFloat(event,this);"/>

Espero que siga siendo util, saludos

Se confirma que sigue gustando. Seria mucha molestia incluir que como mínimo también tenga 2 decimales?

@SantiagoHuh
Copy link

El código está genial, me funciono, pero duda como limito para que solo sea un entero y dos decimales ejemplo 1.40.

@JGuerreroS
Copy link

<input type="text" name="moneda nac" id="moneda_nac" value="10" onkeypress="return filterFloat(event,this);"/>

Como se haría para reemplazar el monto, sin tener que borrar todo el contenido del input?. Ejemplo, cuando escribes por ejemplo 5.55 y quieres reemplazar el entero, tienes que ir borrando todo hasta que llega al numero entero

@jrodrigo93
Copy link

Alguien sabe como editar el monto sin tener que borrar todo el contenido del input y volver a digitar el valor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment