Html Text Input Allow Only Numeric Input


Applications expecting numeric values for the input is generally a pain for developers. Because we have to check given values whether they are number or alphabet. We have some text input element for html. We want to get the number of the person. How can we achieve that easily?

Pure Javascript

By using javascript events is the most reliable method. We will write some javascript for onkeypress event and check the key code. This is a simple code where we check the entered values ASCII codes and if they are inside the number range we return them.

  • onkeypress is the event will be triggered
  • return will return code if the code is number which is between 48 and 57


Html5 provides natively input filtering for various types. Here is the for numbers.

At least keep in mind that to make things more secure server side filtering is a must. Do not values provided by the client side.

LEARN MORE  How To List Shutdown Events In Linux?

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)