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

后端 未结 15 624
情话喂你
情话喂你 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 <input type="number" /> 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: <input type="number" ng-model="foo" replace-comma /> will substitute commas with dots on-the-fly to prevent users from inputting invalid (from a javascript standpoint, not a locales one!) numbers. Cheers.

    0 讨论(0)
  • 2020-11-29 03:33

    HTML step Attribute

    <input type="number" name="points" step="3">
    

    Example: if step="3", legal numbers could be -3, 0, 3, 6, etc.

     

    Tip: The step attribute can be used together with the max and min attributes to create a range of legal values.

    Note: The step attribute works with the following input types: number, range, date, datetime, datetime-local, month, time and week.

    0 讨论(0)
  • 2020-11-29 03:35

    As far as I understand it, the HTML5 input type="number always returns input.value as a string.

    Apparently, input.valueAsNumber returns the current value as a floating point number. You could use this to return a value you want.

    See http://diveintohtml5.info/forms.html#type-number

    0 讨论(0)
  • 2020-11-29 03:36

    Have you considered using Javascript for this?

    $('input').val($('input').val().replace(',', '.'));

    0 讨论(0)
  • 2020-11-29 03:36

    use the pattern

    <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."
    >
    

    good luck

    0 讨论(0)
  • 2020-11-29 03:38

    Currently, Firefox honors the language of the HTML element in which the input resides. For example, try this fiddle in Firefox:

    http://jsfiddle.net/ashraf_sabry_m/yzzhop75/1/

    You will see that the numerals are in Arabic, and the comma is used as the decimal separator, which is the case with Arabic. This is because the BODY tag is given the attribute lang="ar-EG".

    Next, try this one:

    http://jsfiddle.net/ashraf_sabry_m/yzzhop75/2/

    This one is displayed with a dot as the decimal separator because the input is wrapped in a DIV given the attribute lang="en-US".

    So, a solution you may resort to is to wrap your numeric inputs with a container element that is set to use a culture that uses dots as the decimal separator.

    0 讨论(0)
提交回复
热议问题