Skip to content

Instantly share code, notes, and snippets.

@IsaacAndres
Created January 24, 2020 03:02
Show Gist options
  • Save IsaacAndres/e703b57b3004136d0ed1cb25915d7914 to your computer and use it in GitHub Desktop.
Save IsaacAndres/e703b57b3004136d0ed1cb25915d7914 to your computer and use it in GitHub Desktop.
Permite solo un punto y un decimal
<input type="text" onkeypress="return filterFloat(event,this);">
<script>
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 = /^([1-7]\.?[0-9]{0,1})$/;
if(preg.test(__val__) === true){
return true;
}else{
return false;
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment