Skip to content

Instantly share code, notes, and snippets.

@gladiatorAsh
Created June 18, 2015 11:00
Show Gist options
  • Save gladiatorAsh/9ef6f427b7d53aeb2471 to your computer and use it in GitHub Desktop.
Save gladiatorAsh/9ef6f427b7d53aeb2471 to your computer and use it in GitHub Desktop.
function validateFloatKeyPress(el, evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var number = el.value.split('.');
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
//just one dot
if (number.length > 1 && charCode == 46) {
return false;
}
//get the carat position
var caratPos = getSelectionStart(el);
var dotPos = el.value.indexOf(".");
if (caratPos > dotPos && dotPos > -1 && (number[1].length > 2)) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment