JAVASCRIPT - HOW TO Force Numbers Only Input Text - Very Little JS Code

JAVASCRIPT - HOW TO Use this One Line of Code for Input type="text" to [1] Allow Numbers. [2] Enforce a Max Length Regardless of The Input maxlength Property.

Home Short:

Very little Javascript code is needed to perform the following numbers only formatting for input type="text" ...

  1. Allow only numbers to be typed into the textbox.
  2. Prevent spaces in the textbox.
  3. Enforce a maxlength value on the textbox without a maxlength setting on the input type="text" itself.

Try It Yourself

Try entering anything but numbers in the textbox below. Also, change the value of maxwidth and experiment. Feel free to view this pages source code to see the Javascript code for yourself.




S
H
A
R
E