Created
June 18, 2015 11:00
-
-
Save gladiatorAsh/9ef6f427b7d53aeb2471 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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