How to handle floats and decimal separators with html5 input type number

devha picture devha · Mar 8, 2013 · Viewed 217.1k times · Source

Im building web app which is mainly for mobile browsers. Im using input fields with number type, so (most) mobile browsers invokes only number keyboard for better user experience. This web app is mainly used in regions where decimal separator is comma, not dot, so I need to handle both decimal separators.

How to cover this whole mess with dot and comma?

My findings:

Desktop Chrome

  • Input type=number
  • User enters "4,55" to input field
  • $("#my_input").val(); returns "455"
  • I can not get the correct value from input

Desktop Firefox

  • Input type=number
  • User enters "4,55" to input field
  • $("#my_input").val(); returns "4,55"
  • Thats fine, I can replace comma with dot and get correct float

Android browser

  • Input type=number
  • User enters "4,55" to input field
  • When input loses focus, value is truncated to "4"
  • Confusing to user

Windows Phone 8

  • Input type=number
  • User enters "4,55" to input field
  • $("#my_input").val(); returns "4,55"
  • Thats fine, I can replace comma with dot and get correct float

What are the "best practices" in this kind of situations when user might use comma or dot as decimal separator and I want to keep html input type as number, to provide better user experience?

Can I convert comma to dot "on the fly", binding key events, is it working with number inputs?

EDIT

Currenlty I do not have any solution, how to get float value (as string or number) from input which type is set to number. If enduser enters "4,55", Chrome returns always "455", Firefox returns "4,55" which is fine.

Also it is quite annoying that in Android (tested 4.2 emulator), when I enter "4,55" to input field and change focus to somewhere else, the entered number get truncated to "4".

Answer

natchiketa picture natchiketa · May 21, 2013

I think what's missing in the answers above is the need to specify a different value for the step attribute, which has a default value of 1. If you want the input's validation algorithm to allow floating-point values, specify a step accordingly.

For example, I wanted dollar amounts, so I specified a step like this:

 <input type="number" name="price"
           pattern="[0-9]+([\.,][0-9]+)?" step="0.01"
            title="This should be a number with up to 2 decimal places.">

There's nothing wrong with using jQuery to retrieve the value, but you will find it useful to use the DOM API directly to get the elements's validity.valid property.

I had a similar issue with the decimal point, but the reason I realized there was an issue was because of the styling that Twitter Bootstrap adds to a number input with an invalid value.

Here's a fiddle demonstrating that the adding of the step attribute makes it work, and also testing whether the current value is valid:

TL;DR: Set the a step attribute to a floating-point value, because it defaults to 1.

NOTE: The comma doesn't validate for me, but I suspect that if I set my OS language/region settings to somewhere that uses a comma as the decimal separator, it would work. *note in note*: that was not the case for OS language/keyboard settings *German* in Ubuntu/Gnome 14.04.