Force decimal point instead of comma in HTML5 number input (client-side)

后端 未结 15 708
情话喂你
情话喂你 2020-11-29 03:05

I have seen that some browsers localize the input type=\"number\" notation of numbers.

So now, in fields where my application displays longitude and la

15条回答
  •  抹茶落季
    2020-11-29 03:53

    I needed to ensure values can still be entered with a comma instead of a point as a decimal separator. This seems to be an age-old problem. Background info can be found following these links:

    • https://www.slightfuture.com/webdev/html5-input-number-localization.html
    • https://codepen.io/aminimalanimal/full/bdOzRG

    I finally solved it with a little bit of jQuery. Replacing the commas with dots onChange. This seems to be working good so far in latest Firefox, Chrome and Safari.

    $('input[type=number]').each(function () {
    
      $(this).change(function () {
    
        var $replace = $(this).val().toString().replace(/,/g, '.');
    
        $(this).val($replace);
    
      })
    
    });
    

提交回复
热议问题