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

HtmlInputNumbers

Html Problem Overview


I have the following html5 input element:

<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)

Using chrome v. 44.0.2403.107

Example below to see what I mean.

<input type="number">

Html Solutions


Solution 1 - Html

Because that's exactly how the spec says it should work. The number input can accept floating-point numbers, including negative symbols and the e or E character (where the exponent is the number after the e or 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".

Solution 2 - Html

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

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

Solution 3 - Html

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

<input type="number" onkeydown="javascript: return ['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code) ? true : !isNaN(Number(event.key)) && event.code!=='Space'" />

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

To allow number keys only, convert to number with Number function. If this is not a number, the result is NaN : > isNaN(Number(event.key))

but accept Backspace, Delete, Arrow left, Arrow right : > ['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code)

This is for Firefox which allows spaces : > event.code!=='Space'

Solution 4 - Html

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

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

Solution 5 - Html

<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();
	}
};

Solution 6 - Html

A simple solution to exclude everything but integer numbers

<input  
    type="number"
    min="1" 
    step="1"
    onkeypress="return event.keyCode === 8 || event.charCode >= 48 && event.charCode <= 57">

This solution does not prevent copy and pasting (including the letter 'e').

Solution 7 - Html

To hide both letter e and minus sign - just go for:

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

Solution 8 - Html

Using angular, You can do this to restrict to enter 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;

  }

Solution 9 - Html

The E stands for the exponent, and it is used to shorten long numbers. Since the input is a math input and exponents are in math to shorten great numbers, so that's why there is an E.

It is displayed like this: 4e.

Links: 1 and 2

Solution 10 - Html

Angular; with IDE keyCode deprecated warning

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

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);
}

Solution 11 - Html

The above solutions work in regular html only. For reactJS I would suggest you to do this instead

  <input type="number" onKeyDown={(e) =>["e", "E", "+", "-"].includes(e.key) && e.preventDefault()} >

Solution 12 - Html

Simple and standard solution : In Angular/ Js/ Ts you can use regular expression to restrict any input key.

HTML: <input type="text" name="input1" (keypress)="numericOnly($event)" />

TS:

    numericPattern = /^[0-9]*$/;
    numericOnly(event){
       return this.numericPattern.test(event.key);
    }

Solution 13 - Html

if you hit a key and its computer language equivalent is 69 it won't type it

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGnarlywhaleView Question on Stackoverflow
Solution 1 - Htmlj08691View Answer on Stackoverflow
Solution 2 - HtmlyasaruiView Answer on Stackoverflow
Solution 3 - HtmlA. MorelView Answer on Stackoverflow
Solution 4 - HtmlAnon CypherView Answer on Stackoverflow
Solution 5 - Htmluser3248578View Answer on Stackoverflow
Solution 6 - HtmlJelleView Answer on Stackoverflow
Solution 7 - HtmlAustris CirulnieksView Answer on Stackoverflow
Solution 8 - Htmlrinku ChoudharyView Answer on Stackoverflow
Solution 9 - HtmlSurView Answer on Stackoverflow
Solution 10 - HtmlSimon LeggView Answer on Stackoverflow
Solution 11 - HtmlAlexRGView Answer on Stackoverflow
Solution 12 - HtmlPrasad BhaleraoView Answer on Stackoverflow
Solution 13 - HtmlBerkay NaymanView Answer on Stackoverflow