HTML Number Input removes decimal point?

試著忘記壹切 提交于 2019-12-01 04:24:34

Support to <input type=number> is still very limited, incomplete, and buggy.

For example, there is no support on IE 10 and Firefox 19. On them, the element falls back to an <input type=text> element, which means that any string is accepted as input and passed to the server as such, with no checking.

On browsers that support it, such as Chrome, the behavior varies, and is expected to vary, since the HTML5 CR definition intentionally leaves it open: the browser is required to provide a widget for numeric input somehow and guarantee that the data eventually sent to the server is correct as per the attributes of the element (within the given range, etc.).

In practice, the implementation depends on the locale of the browser, and you cannot control that as an author. This means that a browser might accept 88.2, or it might accept 88,2 (and internally convert it to 88.2), or it might accept both, or it might, in principle, accept input in hieroglyphs only (and internally convert it to the canonical form). It might even parse the user input so that any extra characters are just discarded, which means that 88FOO as well as 88.2 might be truncated to 88 when comma is the decimal separator.

What happens in your case is difficult to decide, since the browsers and platforms have not been described, and it is unclear what the server gets. But the important thing is that <input type=number> cannot be relied on, at all. It can be used in a controlled environment where everyone uses the same version of the same browser in the same operating system, up to and including language version and locale settings. Otherwise, use <input type=text> for numeric input and parse the input server-side and optionally pre-check it client-side, and make sure you inform the user of the expected format of input (such as decimal comma vs. decimal point).

type='number' step='any' may solve the issue.

http://dev.w3.org/html5/markup/input.number.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!