为什么输入类型=数字允许多个小数? [英] Why does input type=number allow multiple decimals?

查看:137
本文介绍了为什么输入类型=数字允许多个小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么,但我很困惑,为什么它允许多个小数。

如果您查看有效的浮点数,你会得到:

lockquote

字符串是一个<如果它包含:


  1. 可选的是 - (U + 002D)字符。
  2. 以下的一个或两个按给定顺序:


    1. 一系列一个或多个ASCII数字。


      1. 一个。 (U + 002E)字符。
      2. 一系列一个或多个ASCII数字。



    $ b $ ol

  3. 可以是e(U + 0065)或E(U
  4. 可选择使用 - (U + 002D)字符或+(U + 002B)字符。

  5. 一系列一个或多个ASCII数字。



我在这里看到的所有东西都表示在输入中应该只允许一个小数点,并且在之前必须包含,并用e指定一个指数,并且只能有一个e,无论大小写。

然而,允许输入这些内容:


  • 1 ... .2

  • ......

  • eeee ........

  • 1.0.0.01.0



等等。

在IE 11,它会让我输入任何字符串,但除非是有效数字,否则 .value 。在这种情况下, 1 ...... e 是有效的,但是 eeeeee 不是。如果IE确定该值无效,那么在远离输入焦点时显示屏会消失,从而阻止用户修改现有输入。

在Chrome 51中,它只会让我输入数字,+/-,e / E和。。当我使用一个额外的小数检查像 10.0。这样的输入值时,它仍然返回 10.0 的值,但是如果我输入 10.0.0 10 .. ,字符串将恢复为空。 Chrome会保留显示无效输入。



当字符串变空时,它可以防止对输入进行进一步的验证检查并给予用户有用的反馈。

为什么在数字输入字段中允许多个小数位开头,为什么他们在浏览器之间处理得如此奇怪?

解决方案

p>我做了一些挖掘,并且我有一种预感,认为这可能是Chrome的数字输入实现中的一个错误。



以下是修改了哪些字符被允许输入类型编号的变更集。


$

  event-> setText(locale()。stripInvalidNumberCharacters(event- > text(),0123456789.Ee- +)); 

这意味着 0123456789.Ee - + 根据Chrome浏览器都是合法的字符。其他一切都被剥离出来,但是这些角色是可以通过的。但是,没有检查输入的值是否是真实的实际数字,并且您可以输入由这些字符组成的无意义字符串,并且这些字符串仍然被认为是有效的(例如 +++ 111ee ... ++++ ... +++ ... +++ ... +++ 123321 或类似的东西)。



在允许任何字符的数字输入问题的原始票证中,似乎这可能是作者的意图。 您可以在Chromium的问题跟踪器上查看原始问题。



无论哪种方式,使用Chromium项目提交错误报告都可能是值得的,让他们知道有些事情正在发生。


I understand why number inputs allow "e" or "E", but I'm confused as to why it allows multiple decimals.

If you look at the specification for valid floating-point numbers, you get:

A string is a valid floating-point number if it consists of:

  1. Optionally, a "-" (U+002D) character.
  2. One or both of the following, in the given order:

    1. A series of one or more ASCII digits.
      1. A single "." (U+002E) character.
      2. A series of one or more ASCII digits.

  3. Optionally:

    1. Either a "e" (U+0065) character or a "E" (U+0045) character.
    2. Optionally, a "-" (U+002D) character or "+" (U+002B) character.
    3. A series of one or more ASCII digits.

Everything I see here indicates that only a single decimal point should be allowed in the input, and that decimal has to be included before specifying an exponent with "e", and that there can only be one "e", regardless of case.

Yet, inputs such as these are allowed:

  • 1.....2
  • ......
  • eeee........
  • 1.0.0.01.0

And so on.

In IE 11, it'll let me type in whatever string I what, but unless it's a "valid" number the .value is "". In this case, 1......e is valid, but eeeeee is not. If IE determines the value is not valid, then on focus away from the input the display blanks out, preventing the user from modifying the existing input.

In Chrome 51, it will only let me type digits, +/-, e/E and ".". When I check the value of input like 10.0. with an extra decimal, it still returns the value of 10.0, but if I type 10.0.0 or 10.. the string reverts to empty. Chrome will preserve displaying the invalid input.

When the strings become empty, it prevents doing further validation checks of the input and giving helpful user feedback.

So, why are multiple decimals allowed in the number input fields to begin with, and why are they handled so oddly between browsers?

解决方案

I did some digging, and I have a hunch that this may be a bug in Chrome's implementation of the number input.

Here's the changeset where they fixed which characters were allowed in an input of type number.

Of note is this line:

event->setText(locale().stripInvalidNumberCharacters(event->text(), "0123456789.Ee-+"));

That means that 0123456789.Ee-+ are all legal characters according to Chrome. Everything else gets stripped out, but those characters are allowed through. However, there's no checking on whether or not the value of the input is a real actual number and you can enter nonsensical strings made up of those characters and have those strings still be considered valid (e.g. +++111ee... or ++++ or ...+++...+++...+++123321 or anything like that).

In the original ticket for the issue of number inputs allowing any characters, it seems this might be the author's intent. You can check the original issue on Chromium's issue tracker.

Either way, it might be worth filing a bug report with the Chromium project to let them know that something funky is going on.

这篇关于为什么输入类型=数字允许多个小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆