I created this script that will not let users to enter more than two decimal places in an input field.
var limit = 2;
jQuery("input.number").keydown(function(e) {
if(!isNaN(String.fromCharCode(e.which))){
var inputVal = jQuery(this).val();
if(inputVal.indexOf(".") > -1){
var splitted = inputVal.split(".");
var decimalCount = splitted[1].length;
if(decimalCount >= limit){
e.preventDefault();
}
}
}
});
The script works perfectly on desktop but when I test it in Chrome for Android, it doesn't seem to work. I tried changing the event to "on", "keypress(which is already deprecated)" but none of the options resolve the issue. Can someone please help me out?
Update 1:
I just noticed the script doesn't work for numpads only even in desktop. If I try to type from keyboard's right numpad, the script doesn't work. On the other hand, if I type the numbers from the top numbers right below the functional keys "f1-f12", the script works. That said, in mobile too, it opens numpad. Guess that's where the problem lies.