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

后端 未结 15 712
情话喂你
情话喂你 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:33

    I don't know if this helps but I stumbled here when searching for this same problem, only from an input point of view (i.e. I noticed that my was accepting both a comma and a dot when typing the value, but only the latter was being bound to the angularjs model I assigned to the input). So I solved by jotting down this quick directive:

    .directive("replaceComma", function() {
        return {
            restrict: "A",
            link: function(scope, element) {
                element.on("keydown", function(e) {
                    if(e.keyCode === 188) {
                        this.value += ".";
                        e.preventDefault();
                    }
                });
            }
        };
    });
    

    Then, on my html, simply: will substitute commas with dots on-the-fly to prevent users from inputting invalid (from a javascript standpoint, not a locales one!) numbers. Cheers.

提交回复
热议问题