Allow only numbers in an input

5

I have two input fields, latitude and longitude. In it I should only allow the entry of numbers, sign - and. how can I make a function for it?

    
asked by anonymous 07.10.2016 / 14:03

1 answer

7

Using this function, you can limit the number of keys that the user can type, in their case: numbers, sign ( - ) and period ( . )

var filtroTeclas = function(event) {
  return ((event.charCode >= 48 && event.charCode <= 57) || (event.keyCode == 45 || event.charCode == 46))
}
label {
  float: right;
}
<table>
  <tr>
    <td>
      <label for="latitude">Latitude:</label>
    </td>

    <td>
      <input type="text" id="latitude" onkeypress='return filtroTeclas(event)' />
    </td>
  </tr>
  <tr>
    <td>
      <label for="latitude">Longitude:</label>
    </td>

    <td>
      <input type="text" id="latitude" onkeypress='return filtroTeclas(event)' />
    </td>
  </tr>
</table>
    
07.10.2016 / 14:21