1

After doing a lot of Google searching I have found lot's of folks with the same problem I am running into, but no solutions. I am using Phonegap to build an Android app. I have an input field with type set to number (this allows the numeric keyboard to pop up). If I enter 0.1 life is good. If I type .1 into the input box I get a blank field returned. I tried this bit of code, which works on the web version, but not on the app to try and fix the problem:

    $('#merchantInput input[type=number]').on('blur',function(){
    var input_regex = new RegExp(/^\.{1}\d+/);  
    var test = input_regex.exec($(this).val());
    if(test != null){
        new_num = '0' + $(this).val();
        parseFloat($(this).val(new_num)).toFixed(2);
    }
});

Still, nothing. Any idea's of how to fix this issue?

Just tried this too:

    $('#merchantInput input[type=number]').on('blur',function(){
        var new_num = parseFloat(($(this).val() * 10) / 10).toFixed(2);
        $(this).val(new_num);
});

I am closer....when you enter .3 I get 0.00.

BriKau
  • 33
  • 6

2 Answers2

0

How about checking on change for the value of '.' and replace instantly with '0.'? That way if some one tries to enter '.1' it is automatically changed to '0.1'.

You could try using an eventHandler for 'keyup' which would be more immediate than blur:

$('#merchantInput input[type=number]').on('keyup', function() {
    if($(this).val() == '.') {
        $(this).val('0.');
    }
};
Dawson Loudon
  • 6,029
  • 2
  • 27
  • 31
  • Edited my answer to show using keyup which is more immediate than blur – Dawson Loudon Jan 13 '14 at 22:23
  • Didn't work. I appreciate it though. I think the issue is how Phonegap is compiling. If I remember correct native Java has a numeric and decimal input type. Phonegap, I believe is compiling only as numeric, so that the input field does not recognize a period as the first character. – BriKau Jan 13 '14 at 22:40
0

I had the same problem and after a little googling around (and seeing this post at the top of many searches - hence posting for others) found this answer: https://stackoverflow.com/a/19012837/6466287

basically you use the step attribute to define how the number is allowed to be incremented:

<input type="number" step="0.001" min="0" max="200" inputmode="numeric" pattern="\d+(\.\d*)?" name="weight">

this would allow decimals to 3 decimal places.

Dan Lewis
  • 11
  • 1