-2

I wonder how am I use type=number that can type just 0-9 without 'e' in javascript.

<Field 
    name="weight" 
    type="number" 
    component={TextField} 
    placeholder="" 
    label="Weight(%)"  
    width={250} />

Thank you

Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
ppppp
  • 199
  • 1
  • 2
  • 15
  • There no is field tag. –  Mar 30 '18 at 07:48
  • The question is most likely about how to avoid the *scientific notation* for an `input` field, it's just phrased poorly (likely because OP did not know the correct terminology for that `e` character). I believe an answer can be found [here](https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript), although it will most likely require using an input with type `text`, which will then require converting from the number to text (without scientific notation) and back. – John Weisz Mar 30 '18 at 07:56

1 Answers1

0

One can do it using JavaScript by incorporating type="text" instead of number and applying a conditional check on keypress event as below:

<div>
  <h4>Allow Only Numbers </h4>
  <input type="text" name="onlyNumbers" id="numberField" />
</div>
<script>
  function allowNumbersOnly(e) {
    var keyCode = e.which;
    if (keyCode < 48 || keyCode > 57) { // keyCode for '0' is 48 and for '9' it is 57
      e.preventDefault();
    }
  }
  document.getElementById('numberField').addEventListener('keypress', allowNumbersOnly);
</script>
Shashank
  • 2,010
  • 2
  • 18
  • 38