-1

EDIT : THE TITLE IS MISLEADING since after some testing it turns out that Number() returned a dot-separated number and it was the HTML <input type="number"/> which visualized it as comma due to locale settings. I then resorted to use a <input type="text"/> and filter keydown just like the jQuery version of this answer: html-text-input-allow-only-numeric-input.

ORIGINAL QUESTION: I need to put a number from javascript into an <input type="number"/> html tag using AngularJS ng-model. I've got a number-like string with . as decimal separator, and putting directly the string into the input obviously gives me an error ( numfmt ).

Problem is, I need the number to be dot-separated but both Number() and parseFloat() only return a comma-separated number (rightly so, according to italian locale).

How can I retain the dot-separation without having to revert to <input type="text"/>? I need the automatic input-control on the type="number" and I don't want to use libraries for just this little thing.

I feel like this ought to be a key feature in js but for the life of me I can't find anything on the web.

Community
  • 1
  • 1
Kitra
  • 13
  • 6
  • "_`Number()` and `parseFloat()` only return a comma-separated number_" I'm pretty sure they don't. – Teemu Feb 22 '17 at 11:44
  • To avoid confusion it would be helpful if you could provide examples. – Ivan Modric Feb 22 '17 at 11:45
  • @Teemu they do in a italian-locale browser, as I specified. For the same reason I can't provide examples since this issue is tied to the browser's locale – Kitra Feb 22 '17 at 11:55
  • I still don't believe JS functions would return you comma separated decimals. It's that you see the values in the input element as comma separated, but when reading the value, it is dot separated. My locale (FI) also uses comma as a decimal delimiter, I haven't got any problems with that. – Teemu Feb 22 '17 at 12:00
  • Hmm.. "_... when reading the value [to a JS variable]_". I'd really like to see a code snippet with which we can reproduce the issue. – Teemu Feb 22 '17 at 12:26
  • @Teemu, after some testing I found out you're right, it's a visualization issue in the HTML input element, I'll edit the question (or is it better to create a new one?) – Kitra Feb 22 '17 at 13:04

2 Answers2

0

Here is a way there I can convert your dot to comma on keyPress event.

here is working fiddle here

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
  • Mine is not a validation issue, the problem is that i need to put a dot-separated number into the input, and js automatically converts a number-like string with a dot to a comma-separated one according to my (italian) locale – Kitra Feb 22 '17 at 12:03
  • check again my answer I use keypress event and convert your dot to comma. – Durgpal Singh Feb 22 '17 at 12:12
  • This fiddle is cool and I'll probably use it somewher else, but you didn't catch my question. I don't care if the user changes the dot to a comma, my issue is that when the angular model (changed from the controller js-side, not the user) contains a dot, the HTML input element automatically visualizes it as a comma – Kitra Feb 22 '17 at 13:10
0

Just try this

<input type="number" step="any" />

I hope you this link will helps you : https://www.isotoma.com/blog/2012/03/02/html5-input-typenumber-and-decimalsfloats-in-chrome/

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234