62

I work on a web application running in Chrome, where I have inputs with type number. In my locale commas are used for decimal numbers and a space is used for thousand separation (not that important), but when I enter these characters into a number field, they are simply removed, effectively increasing money amounts by a hundred.

I have set the language both in the browser settings and on the page, but I still need to use a period for decimals. Is there any way I can configure the field to accept commas?

Alternatively, I'll have to solve this using javascript. I guess I could handle the keydown event and change commas to periods as the user types, but that wouldn't give a great user experience, would it? So how can I acheive this with a minimal footprint in my code?

Braiam
  • 1
  • 11
  • 47
  • 78
Jørgen
  • 8,820
  • 9
  • 47
  • 67

5 Answers5

43

The HTML5 input type=number is inadequate from the localization point of view, due to both the definition and the implementations. It is meant to be localized but as per the locale of the browser, which you cannot set or even know as a designer/author.

On my Chrome, the input type=number step=0.001 accepts 1,2 (with comma) and sends it as 1.2 and it accepts 1.200 (with a period), visibly converting it to 1200 and sending as such. This is how things are meant to be, more or less, when the browser locale is Finnish. But it fails to accept 1 200 (which is standard way of writing 1200 in Finnish) and instead sends just the digit 1.

So it’s rather hopeless. Use whatever JavaScript widgets you can find, or a simple text input box. Anything is probably better than input type=number unless all users use browsers with the same locale and have the same expectations on the format of numbers.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
35

If you don't need the up/down ticks, than follow workaround can help:

for comma (,) only (like german syntax):

<input type="text" pattern="[0-9]+([,][0-9]{1,2})?" name="amount">

dot (.) only:

<input type="text" pattern="[0-9]+([\.][0-9]{1,2})?" name="amount">

both but don't together: (no 1000 seperator)

<input type="text" pattern="[0-9]+([\.|,][0-9]{1,2})?" name="amount">

otherwise number for German/Deutsch:

<input name="myinput" value="0" step="0.01" lang="de-DE" type="number">

and style it with:

input[type=number] {
    -moz-appearance:textfield;
    -webkit-appearance: none;
    appearance: textfield;
}

Also lang "global" attribute can change behavior (thx @florian) of all input elements without own lang attribute:

<html lang="en">

See:

List of valid lang values: https://github.com/libyal/libfwnt/wiki/Language-Code-identifiers

Frank
  • 1,901
  • 20
  • 27
  • 1
    Please also consider that on mobile devices the control for input type="number" may look different (number keyboard) and you won't get that extra control for type="text". Also see http://blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html – Stefan Mar 18 '16 at 10:18
  • 1
    I'm in the same situation as the OP and I personally found that the attribute in Frank's answer: lang="de-DE" (or another culture) was exactly what I needed. Frank's answer here should have more upvotes as its a working solution and very simple. – Jeff Mergler Jan 07 '17 at 00:58
  • German comma only is look like ok but.. how you post? because posted value should be culture specific. – Nuri YILMAZ Jul 10 '18 at 20:56
  • Also upvote because of the lang-attribute - i also had the problem that i couldn't directly set the attribute on the input field... it also works when you set the lang-attribute on the html-tag – Florian Mar 23 '21 at 14:57
  • 1
    my browser(chrome) language is set to English but I would like to display float numbers in textinput in German number format. I have tried to add attribute lang="de-DE" in input and HTML tags but doesn't work properly. – vsam May 20 '21 at 08:08
  • 5
    The "lang" attribute doesn't work at all on Chromium based browsers – II ARROWS May 23 '21 at 09:19
  • The "lang" attribute _does_ work on Chromium 114 for me. I get transparently de-DE formatted numbers ("," as decimal separator) – pixelistik Jun 27 '23 at 21:34
12

The spec is clear: only a period is allowed as the decimal separator. Its up to the browsers to provide localization support for forms. Thousand separators are not allowed.

cweiske
  • 30,033
  • 14
  • 133
  • 194
Daniel
  • 4,525
  • 3
  • 38
  • 52
  • 3
    Nice blog post :) I see that it has been some development in this area since I posted the question. – Jørgen Feb 25 '15 at 06:30
  • 2
    This one is nice as well: https://codepen.io/aminimalanimal/full/bdOzRG – leymannx Mar 30 '20 at 15:49
  • Localization is indeed interesting. I have set my `` to have `lang="de-DE"`. When I enter `1,2` into the input field, I get the following results: `.value` is `"1.2"`, so it is a string, but the decimal separator has been un-localized. `valueAsNumber` is `1.2`, so a correct number. But there is **no way** to access the raw `1,2` as a string, as asked in the initial question. – pixelistik Jun 27 '23 at 21:50
3

Unfortunately these characters are not allowed in the <input type="number">

See the specs here : http://w3c.github.io/html-reference/datatypes.html#common.data.float-def

Is this the format you want ? http://jsfiddle.net/S8rqY/

wakooka
  • 1,398
  • 10
  • 15
0

While Chrome uses the Browser setting, Firefox doesn't. At least not always - e.g. when there is a lang attribute in the <html> tag, Firefox uses this.

However, you can pass the lang attribute also to the <input> tag directly. Combining this with the Navigator API can simulate Chromes Behaviour.

Minimum example in React:

<input
  type="number"
  lang={navigator.language}
/>