314

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">
Sur
  • 191
  • 2
  • 3
  • 14
Gnarlywhale
  • 4,030
  • 2
  • 15
  • 18
  • 6
    It also allows you to enter +, -, and `.` multiple times in some browsers. – nu everest Sep 11 '16 at 20:27
  • The [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number) warn about this and points to a [lengthy discussion](https://bugzilla.mozilla.org/show_bug.cgi?id=1398528) of the issue. – Eric Mutta Jun 26 '22 at 22:52

16 Answers16

270

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".
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 8
    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... – Simon Apr 19 '16 at 15:31
  • @Simon It's the exponent, you can see it in action if you multiply a large number by a large number and the calculator doesn't have enough supported digits to display it. Someone who is better at mathematics might explain it better. – ZeroBased_IX Apr 26 '16 at 09:35
  • 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` – Gnarlywhale Jul 21 '16 at 16:38
  • Shouldn't the `input.value` then return `200` if you type in `2e2`? – Simon Jul 22 '16 at 12:42
  • @Simon depends whether the value is string or numeric value. If it's numeric, 2e2 *is* 200, those are equal values and it doesn't matter which form of presentation you use to display it, it's still 0b11001000 either way. If the input is string, then well, convert it to number first, so you don't have to bother which format was used to input the value. – Ped7g Jan 12 '17 at 18:37
  • 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". – Simon Jan 13 '17 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 Jan 13 '17 at 11:18
  • I just discovered this. I'm assuming the 'e' character is possibly used to indicate 'extension' for phone numbers since `type=tel` isn't commonly adopted – oldboy Sep 25 '17 at 06:31
  • 5
    @Anthony No, `e` stands for Exponent. – Scott Marcus Apr 05 '18 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" />
yasarui
  • 6,209
  • 8
  • 41
  • 75
  • 24
    Better to use `return event.keyCode !== 69` as it avoids an unnecessary ternary operator. Also wouldn't recommend inlining. – Adam Fratino Jan 31 '18 at 21:26
  • 12
    This does not prevent copy-pasting `e` or `E` in the field though – molamk Sep 05 '19 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? – user1063287 Jul 20 '20 at 08:50
  • 1
    @user1063287, this is the only exception in the case – yasarui Jul 20 '20 at 11:16
  • 3
    Final code: onkeydown="return event.keyCode !== 69 && event.keyCode !== 187 && event.keyCode !== 189" That prevents e, + and -. – GTS Joe Aug 02 '20 at 16:28
  • @GTSJoe, you need to add 107 and 109 too, its ```+```, ```-``` on numpad :D – simpsons3 Nov 25 '20 at 09:28
  • isnt there a built in configuration so disable this? no one eve uses "e" amazing how they preffered user experience of a few over the majority – Tomas Katz Jul 25 '23 at 12:32
54

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'

A. Morel
  • 9,210
  • 4
  • 56
  • 45
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.          

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Anon Cypher
  • 369
  • 3
  • 3
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();
    }
};
user3248578
  • 905
  • 8
  • 18
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').

Jelle
  • 758
  • 2
  • 14
  • 36
9

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

onkeydown="return event.keyCode !== 69 && event.keyCode !== 189"
Austris Cirulnieks
  • 1,099
  • 11
  • 18
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;

  }
Rinku Choudhary
  • 1,529
  • 1
  • 13
  • 22
  • 2
    this will restrict user to enter that character?? – Amy Oct 30 '19 at 08: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 Oct 30 '19 at 09:05
  • 1
    you can create the directive that is best practice . as well you can also do directly in .ts file of your component! – Rinku Choudhary Oct 31 '19 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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Sur
  • 191
  • 2
  • 3
  • 14
6

valueAsNumber can do the job (copy/paste proof)

    <input type="number" oninput="this.value = this.valueAsNumber">
bilelz
  • 161
  • 2
  • 3
3

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()} >
richinrix
  • 434
  • 1
  • 3
  • 13
AlexRG
  • 51
  • 2
  • 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: https://stackoverflow.com/help/how-to-answer . Good luck – nima Nov 24 '21 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 Apr 18 '22 at 07:03
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"
/>
  • 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](https://stackoverflow.com/help/how-to-answer). – Sfili_81 Mar 17 '22 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);
}
Luís Mestre
  • 1,851
  • 1
  • 11
  • 29
Simon Legg
  • 216
  • 2
  • 3
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

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

bareMetal
  • 425
  • 1
  • 7
  • 20
-5

<input type="number" onkeydown="javascript: return ['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code) ? true : !isNaN(Number(event.key)) && event.code!=='Space'" />
ashish
  • 63
  • 7
  • 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](/help/how-to-answer). – Community Jul 05 '22 at 15:58
  • 1
    this is an exact copy of my answer without explanations! Your answer contributes nothing to the community! – A. Morel Nov 07 '22 at 10:15
  • You are an opportunist! – A. Morel Nov 07 '22 at 10:21