3

I made a table where input is integer, but somehow I am still able to pass any text.

<tr>
      <td>number: </td>
      <td><input type="integer" name="number" value=""/></td>
    </tr>

is the type="integer" correct?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Martin Švejda
  • 83
  • 3
  • 11

3 Answers3

4

In short, integer is not the correct type - what you seek is number

<input type='number' />

Additional attributes for this type of input are possible, including:

  • min: the lowest value the input will allow
  • max: the highest value the input will allow
  • step: the incremental value that the up/down selectors will increment the current value by

More information & example usage from MDN

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
3
<input type="number" name="number" value="" min="0" step="1"/>
Sohel0415
  • 9,523
  • 21
  • 30
1

For numeric, you should use type="number" . For detailed, you can follow the reference .

Your updated code is given as-

<tr>
      <td>number: </td>
      <td><input type="number" name="number" value=""/></td>
    </tr>

Other Solution: you can do it using JavaScript validation as given below.

function isNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
<input type="text"  value="" name="number" onkeypress="return isNumber(event)" />
Aftab H.
  • 1,517
  • 4
  • 13
  • 25