0

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.

Awais Umar
  • 2,027
  • 3
  • 27
  • 46
  • i think you should use keypress for touch events: http://stackoverflow.com/questions/9940829/how-to-capture-the-onscreen-keyboard-keydown-and-keyup-events-for-touch-devi – Stephan Huewe Oct 25 '16 at 06:44
  • *"The script works perfectly on desktop"* - Really? What if the user modifies the field value without using the keyboard? – nnnnnn Oct 25 '16 at 06:46
  • @StephanHuewe, thank you for the link. I'll give it a try right now. – Awais Umar Oct 25 '16 at 06:58
  • @nnnnnn, if you can please explain, I would surely update the script accordingly. Thank you. – Awais Umar Oct 25 '16 at 06:59

1 Answers1

-1

Include jQuery Mobile file

What you need to do is just include a jQuery file into your project. A file named jQuery.mobile.js or quite similar (ex. jQuery.ui.js) of any version can help you.

You can download it from : Download | jQuery Mobile I would like to suggest to use ResolveUrl while giving path for this file.