323

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

2
  • 8
    It also allows you to enter +, -, and . multiple times in some browsers.
    – nu everest
    Commented Sep 11, 2016 at 20:27
  • The MDN docs warn about this and points to a lengthy discussion of the issue.
    – Eric Mutta
    Commented Jun 26, 2022 at 22:52

16 Answers 16

277

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".
9
  • 9
    I'm still baffled by this, first of all I'm not a mathematician, so what does "e" stand for in the context of a number? Second I don't get why input.value is an empty string as soon as you write an "e" in it, even though there are numbers and the character is allowed...
    – Seimen
    Commented Apr 19, 2016 at 15:31
  • 19
    @Simon, using the e is useful for condensing large numbers that would be otherwise tedious to type out. As a trivial example, 2e2 = 2*10^2 = 200 Commented Jul 21, 2016 at 16:38
  • 2
    @Ped7g Well that's exactly the problem! If I want to read out the value to find out it's 200 either way I can't, because input.value will return undefined as soon as I write the "e".
    – Seimen
    Commented Jan 13, 2017 at 11:08
  • 4
    @Simon "as soon as I write e", well, yes "4e" is not a number, while for example "4e+0" is a valid number (4). If you have some "live" client-side javascript code working with partial user input, you have to give user time to finish his input editing to provide full value, and not interfere half way into editing. If you have "undefined" from "4e+0" input, fix your "to number" parser. The example from question works well, reports "4e+1" as error, and "4e+0" is returned correctly as "4e+0" (ie. number between 1 and 5).
    – Ped7g
    Commented Jan 13, 2017 at 11:18
  • 5
    @Anthony No, e stands for Exponent. Commented Apr 5, 2018 at 16:58
103

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" />

7
  • 24
    Better to use return event.keyCode !== 69 as it avoids an unnecessary ternary operator. Also wouldn't recommend inlining. Commented Jan 31, 2018 at 21:26
  • 13
    This does not prevent copy-pasting e or E in the field though
    – molamk
    Commented Sep 5, 2019 at 14:10
  • 1
    are there any other instances similar to e or E that non-mathematicians might not be aware of that could be added to this conditional check? Commented Jul 20, 2020 at 8:50
  • 1
    @user1063287, this is the only exception in the case
    – yasarui
    Commented Jul 20, 2020 at 11:16
  • 3
    👍 Final code: onkeydown="return event.keyCode !== 69 && event.keyCode !== 187 && event.keyCode !== 189" That prevents e, + and -.
    – GTS Joe
    Commented Aug 2, 2020 at 16:28
58

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'

3
  • 4
    Notice that this will be accepting space: the key of space is ' ', Number(' ') is equal to zero
    – ehab
    Commented Sep 17, 2020 at 16:37
  • 1
    adding && !event.key == ' ' solves the problem of spaces.
    – Geff
    Commented May 2, 2021 at 17:31
  • You missed 'Tab' there, preventing user using the Tab key to focus next input element.
    – Roni Tovi
    Commented Mar 2, 2023 at 14:37
36

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.          

14
<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();
    }
};
3
  • 2
    How confident are you that this will prevent inserting an 'E' character with the copy and paste functionality? Commented Jul 21, 2016 at 14:20
  • 1
    Just checked, you are correct, it doesn't work for copy and pasted input. Commented Jul 21, 2016 at 16:34
  • You are correct @StephenKelzer. I added code to handle that. Commented Jul 22, 2016 at 5:03
11

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

2
  • nice answer, but if I also want to allow decimal value in it. can you guide me. Commented Aug 2, 2022 at 8:18
  • To also allow decimal values I suggest taking a look at the answer of @yasarui
    – Jelle
    Commented Aug 3, 2022 at 9:12
10

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

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

valueAsNumber can do the job (copy/paste proof)

    <input type="number" oninput="this.value = this.valueAsNumber">

1
  • 1
    It doesn't work if you type "1" and then paste "e123" after it.
    – Dron007
    Commented Nov 8, 2023 at 5:13
8

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;

  }
3
  • 2
    this will restrict user to enter that character??
    – Amy
    Commented Oct 30, 2019 at 8:57
  • 1
    thanks for the help i was looking for the same solution in angular but now i have idea how to do it, i'll create directive and complete the task thanks for your post
    – Amy
    Commented Oct 30, 2019 at 9:05
  • 1
    you can create the directive that is best practice . as well you can also do directly in .ts file of your component! Commented Oct 31, 2019 at 17:19
8

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

5

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()} >
3
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂
    – nima
    Commented Nov 24, 2021 at 18:49
  • Thanks. I was working in react and this is the only solution that worked for me. The remaining solutions don't work in react.
    – richinrix
    Commented Apr 18, 2022 at 7:03
  • Doesn't work during copy paste, as it allows numbers with 'e' whilst copy paste.
    – Kesava
    Commented Sep 20, 2023 at 11:11
3

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"
/>
1
  • 3
    Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.
    – law_81
    Commented Mar 17, 2022 at 15:31
1

Angular; with IDE keyCode deprecated warning

The functionally is 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);
}
0

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

Simplest solution is parseInt() .

<input type="number" onkeyup="this.value = parseInt(this.value); this.paste(this.onkeyup);"/>

ParseInt() returns blank(NaN) for E, e, +, - and anything which is not a number.

Use onkeyup and keydown according to your convenience with paste event. For me onkeyup works best. This works with copy-paste as well

-5

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

3
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 5, 2022 at 15:58
  • 1
    this is an exact copy of my answer without explanations! Your answer contributes nothing to the community!
    – A. Morel
    Commented Nov 7, 2022 at 10:15
  • You are an opportunist!
    – A. Morel
    Commented Nov 7, 2022 at 10:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.