Javascrìpt: Permitir sólo números en Javascript

Junio 4th, 2007 by kenavik

Os dejo una función para evitar que nos introduzcan valores que son sean númericos en un campo HTML. Esta función funciona correctamente en Firefox e Internet Explorer.

<script language=”javascript”>
function IsNumber(e) {

tecla = (document.all) ? e.keyCode : e.which;

if (tecla==8) return true;

patron = /\d/; // Solo acepta números

te = String.fromCharCode(tecla);

return patron.test(te);

}

</script>

Usamos el evento onKeypress y así cada vez que el usuario pulsa una tecla comprobamos que el valor sea númerico.

<input type=”text” name=”telf” value=”" onkeypress=”return IsNumber(event);” >

Posted in Javascript |

One Response

  1. ROBERTO FLORES Says:

    GRACIAS FUNCIONA DE MARAVILLLA

Escribir un comentario

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.