[JavaScript] Validación campo input text (Solo debe aceptar números)

470 views
0

HTML:

<form name="miformulario">
  <td align=right>Cod.Postal:</td><td align=left><input type="text" name="codigo" id="idcodigo" maxlength="5"></td>
</form>

JavaScript:

window.addEventListener("load", function() {
  miformulario.codigo.addEventListener("keypress", soloNumeros, false);
});

//Solo permite introducir numeros.
function soloNumeros(e){
  var key = window.event ? e.which : e.keyCode;
  if (key < 48 || key > 57) {
    e.preventDefault();
  }
}
About author

PandaGG

Lok'tar ogar

Your email address will not be published. Required fields are marked *