Html 为什么类型为“number”的html输入允许在字段中输入字母“e”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31706611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:51:34  来源:igfitidea点击:

Why does the html input with type "number" allow the letter 'e' to be entered in the field?

htmlinputnumbers

提问by Gnarlywhale

I have the following html5 input element:

我有以下 html5 输入元素:

<input type="number">

Why does this input allow for the character 'e' to be entered in the input field? No other alphabet character is able to be entered (as expected)

为什么此输入允许在输入字段中输入字符“e”?无法输入其他字母字符(如预期)

Using chrome v. 44.0.2403.107

使用 chrome v. 44.0.2403.107

To see what I mean: http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number

明白我的意思:http: //www.w3schools.com/html/tryit.asp?filename= tryhtml_input_number

回答by j08691

Because that's exactly how the specsays it should work. The number input can accept floating-point numbers, including negative symbols and the eor Echaracter (where the exponent is the number after the eor E):

因为这正是规范所说的它应该工作的方式。数字输入可以接受浮点数,包括负eorE字符(其中指数是eor之后的数字E):

A floating-point number consists of the following parts, in exactly the following order:

  1. Optionally, the first character may be a "-" character.
  2. One or more characters in the range "0—9".
  3. Optionally, the following parts, in exactly the following order:
    1. a "." character
    2. one or more characters in the range "0—9"
  4. Optionally, the following parts, in exactly the following order:
    1. a "e" character or "E" character
    2. optionally, a "-" character or "+" character
    3. One or more characters in the range "0—9".

浮点数由以下部分组成,顺序如下:

  1. 可选地,第一个字符可以是“ -”字符。
  2. 0—9”范围内的一个或多个字符。
  3. 可选,以下部分,完全按照以下顺序:
    1. 一个 " ." 字符
    2. " 0—9"范围内的一个或多个字符
  4. 可选,以下部分,完全按照以下顺序:
    1. 一个 " e" 字符或 " E" 字符
    2. 可选,一个“ -”字符或“ +”字符
    3. 0—9”范围内的一个或多个字符。

回答by codegeek

We can make it So simple like below

我们可以让它像下面这样简单

<input type="number"  onkeydown="javascript: return event.keyCode == 69 ? false : true" />

Updated Answer

更新答案

we can make it even more simple as @88 MPG suggests

我们可以像@88 MPG 建议的那样让它变得更简单

<input type="number" onkeydown="return event.keyCode !== 69" />

回答by Anon Cypher

HTML input number type allows "e/E" because "e" stands for exponential which is a numeric symbol.

HTML 输入数字类型允许“e/E”,因为“e”代表指数,它是一个数字符号。

Example 200000 can also be written as 2e5. I hope this helps thank you for the question.          

例 200000 也可以写成 2e5。我希望这有助于感谢您的问题。          

回答by A. Morel

The best wayto force the use of a number composed of digits only:

强制使用仅由数字组成的数字的最佳方法

<input type="number" onkeydown="javascript: return event.keyCode === 8 ||
event.keyCode === 46 ? true : !isNaN(Number(event.key))" />

this avoids 'e', '-', '+', '.' ... all characters that are not numbers !

这避免了“e”、“-”、“+”、“.” ...所有不是数字的字符!

To allow number keys only:

只允许数字键:

isNaN(Number(event.key))

isNaN(Number(event.key))

but accept "Backspace" (keyCode: 8) and "Delete" (keyCode: 46) ...

但接受“退格”(keyCode:8)和“删除”(keyCode:46)......

回答by TruthSeeker

<input type="number" onkeydown="return FilterInput(event)" onpaste="handlePaste(event)"  >

function FilterInput(event) {
    var keyCode = ('which' in event) ? event.which : event.keyCode;

    isNotWanted = (keyCode == 69 || keyCode == 101);
    return !isNotWanted;
};
function handlePaste (e) {
    var clipboardData, pastedData;

    // Get pasted data via clipboard API
    clipboardData = e.clipboardData || window.clipboardData;
    pastedData = clipboardData.getData('Text').toUpperCase();

    if(pastedData.indexOf('E')>-1) {
        //alert('found an E');
        e.stopPropagation();
        e.preventDefault();
    }
};

回答by Austris Cirulnieks

To hide both letter eand minus sign -just go for:

要隐藏字母e和减号,-只需执行以下操作:

onkeydown="return event.keyCode !== 69 && event.keyCode !== 189"

回答by rinku Choudhary

Using angular, You can do this to restrict to enter e,+,-,E

使用角度,您可以这样做来限制输入 e,+,-,E

 <input type="number"  (keypress)="numericOnly($event)"/>


  numericOnly(event): boolean { // restrict e,+,-,E characters in  input type number
    debugger
    const charCode = (event.which) ? event.which : event.keyCode;
    if (charCode == 101 || charCode == 69 || charCode == 45 || charCode == 43) {
      return false;
    }
    return true;

  }

回答by Simon Legg

Angular; with IDE keyCode deprecated warning

角; 带有 IDE keyCode 已弃用警告

Functionally the same as rinku's answer but with IDE warning prevention

功能与 rinku 的答案相同,但具有 IDE 警告预防

numericOnly(event): boolean {
    // noinspection JSDeprecatedSymbols
    const charCode = (event.which) ? event.which : event.key || event.keyCode;  // keyCode is deprecated but needed for some browsers
    return !(charCode === 101 || charCode === 69 || charCode === 45 || charCode === 43);
}