PHP webdeveloper and frontend developer

Allow only numeric input or disallow spaces with Javascript

Category : jQuery & javascript · by Jan 15th, 2012

This is a very easy way to restrict users from inputting any characters other then numbers or restrict users from entering spaces in e.g. login and password fields. You can restrict other characters by adding the charcodes to the function. You can check the charcodes at


Load in the javascript file and add onkeypress=“return isNumberKey(event)” in the input tag to restrict users from entering anything other then numeric input. Add onkeypress=“return nospaces(event)” to restrict users from entering spaces.

like so:

This is the javascript function to use:



Leave a Reply

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