Is there a float input type in HTML5?

前端 未结 9 1798
失恋的感觉
失恋的感觉 2020-11-22 11:32

According to html5.org, the \"number\" input type\'s \"value attribute, if specified and not empty, must have a value that is a valid floating point number.\"

Yet it

相关标签:
9条回答
  • 2020-11-22 12:12

    The number type has a step value controlling which numbers are valid (along with max and min), which defaults to 1. This value is also used by implementations for the stepper buttons (i.e. pressing up increases by step).

    Simply change this value to whatever is appropriate. For money, two decimal places are probably expected:

    <input type="number" step="0.01">
    

    (I'd also set min=0 if it can only be positive)

    If you'd prefer to allow any number of decimal places, you can use step="any" (though for currencies, I'd recommend sticking to 0.01). In Chrome & Firefox, the stepper buttons will increment / decrement by 1 when using any. (thanks to Michal Stefanow's answer for pointing out any, and see the relevant spec here)

    Here's a playground showing how various steps affect various input types:

    <form>
      <input type=number step=1 /> Step 1 (default)<br />
      <input type=number step=0.01 /> Step 0.01<br />
      <input type=number step=any /> Step any<br />
      <input type=range step=20 /> Step 20<br />
      <input type=datetime-local step=60 /> Step 60 (default)<br />
      <input type=datetime-local step=1 /> Step 1<br />
      <input type=datetime-local step=any /> Step any<br />
      <input type=datetime-local step=0.001 /> Step 0.001<br />
      <input type=datetime-local step=3600 /> Step 3600 (1 hour)<br />
      <input type=datetime-local step=86400 /> Step 86400 (1 day)<br />
      <input type=datetime-local step=70 /> Step 70 (1 min, 10 sec)<br />
    </form>


    As usual, I'll add a quick note: remember that client-side validation is just a convenience to the user. You must also validate on the server-side!

    0 讨论(0)
  • 2020-11-22 12:20

    You can use the step attribute to the input type number:

    <input type="number" id="totalAmt" step="0.1"></input>
    

    step="any" will allow any decimal.
    step="1" will allow no decimal.
    step="0.5" will allow 0.5; 1; 1.5; ...
    step="0.1" will allow 0.1; 0.2; 0.3; 0.4; ...

    0 讨论(0)
  • 2020-11-22 12:22

    Via: http://blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/

    But what if you want all the numbers to be valid, integers and decimals alike? In this case, set step to “any”

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

    Works for me in Chrome, not tested in other browsers.

    0 讨论(0)
  • 2020-11-22 12:24

    I do so

     <input id="relacionac" name="relacionac" type="number" min="0.4" max="0.7" placeholder="0,40-0,70" class="form-control input-md" step="0.01">
    

    then, I define min in 0.4 and max in 0.7 with step 0.01: 0.4, 0.41, 0,42 ... 0.7

    0 讨论(0)
  • 2020-11-22 12:24

    I just had the same problem, and I could fix it by just putting a comma and not a period/full stop in the number because of French localization.

    So it works with:

    2 is OK

    2,5 is OK

    2.5 is KO (The number is considered "illegal" and you receive empty value).

    0 讨论(0)
  • 2020-11-22 12:28

    You can use:

    <input type="number" step="any" min="0" max="100" value="22.33">
    
    0 讨论(0)
提交回复
热议问题