4

I've some issues with decimal input on iOS using the numeric keypad. I have the following HTML:

$('#number').keyup(function() {
  $('#log').prepend('<p>Input: ' + $(this).val() + '</p>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" inputmode="decimal" id="number">
<p>
  Input as number:
  <div id="log"></div>
</p>

This is working as expected in Chrome browser, Android etc., but on iOS there is some issues. If I set the Region to e.g. Denmark (comma decimal seperator) but the Language to English (UK) (point decimal seperator), the number pad gives me a comma decimal seperator, but I seems that the HTML/JS does not support this. If I input e.g. 12,3 the value of the input field becomes empty when I use the comma.

How can I fix this?

iOS demo

When Region is Denmark and Language is Danish, it's all working as expected.

The code and demo is available on this StackBlitz: https://decimal-input-ios.stackblitz.io

Spectric
  • 30,714
  • 6
  • 20
  • 43
dhrm
  • 14,335
  • 34
  • 117
  • 183
  • Is the issue present on a specific browser on iOS or all browser? Did you try iOS Firefox or Chrome? – 3limin4t0r Jul 02 '21 at 09:35
  • @3limin4t0r iOS always uses the same browser engine. No matter which browser you use it is always webpack. So the errors are usually there on all iOS Browsers – basti500 Jul 02 '21 at 09:38

5 Answers5

2

I found some workaround, you can replace , with . every time that it is being typed:

let prevNum = "";

$('#number').on("keyup", function (e) {
    if (e.keyCode == 188) {
        $(this).val(prevNum + ".");
    }
    
    prevNum = $(this).val();

    $('#log').prepend('<p>Input: ' + $(this).val() + '</p>');
});
Costa
  • 1,794
  • 1
  • 12
  • 21
0

Can you please try to add lang="en" it should change by adding a lang attribute

<input type="number" inputmode="decimal" id="number" lang="en">
Transformer
  • 6,963
  • 2
  • 26
  • 52
0

This is a cheat, but after reading several similar posts about it, I'm not sure you have too many options. If you don't need the (usually inconsequential) up/down ticks, you can just use a 'text' input with a pattern. The pattern will tell iOS to use the number pad despite this being a 'text' input.

$('#number').keyup(function(evt) {
  $('#log').prepend('<p>Input: ' + $(this).val() + '</p>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" inputmode="decimal" id="number" pattern="[0-9.,]+">
<p>Input as number:</p>
  <div id="log"></div>
Kinglish
  • 23,358
  • 3
  • 22
  • 43
0

Use parseFloat() to make the value a float

$('#number').keyup(function() {
  $('#log').prepend('<p>Input: ' + parseFloat($(this).val()) + '</p>');
});
<html lang="en-GB">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" inputmode="decimal" id="number">
<p>
  Input as number:
  <div id="log"></div>
</p>
</html>
AlexSp3
  • 2,201
  • 2
  • 7
  • 24
0

I cannot test since I do not own any IOS device...

But you said:

When Region is Denmark and Language is Danish, it's all working as expected.

So why not just change the whole page language on that specific input focus and restore it on blur? That would be:

$("#number").on("focus", function(){
  $("html").attr("lang", "da");
});

$("#number").on("blur", function(){
  $("html").attr("lang", "en");
});

It worths a try ;)

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64