Allow only numbers in an input

Asked

Viewed 3,135 times

5

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

1 answer

6


Using this function below you limit the keys that the user can type, in your case: numbers, sign (-) and point ( . )

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.