I want to prevent the user from typing anything other than numbers
Use input
itself of type number
of HTML
The <input type="number">
defines a numeric input field.
You can also set restrictions on which numbers are accepted.
The following example displays a numeric input field, where you can enter a value from 1 to 5:
<form>
<input type="number" name="quantity" min="1" max="5">
</form>
Entry Restrictions
Here is a list of some common input restrictions (some are new to HTML5):
disabled - Specifies that an input field should be disabled
max * - Specifies the maximum value for an input field
maxlength - Specifies the maximum number of characters for an input field
min * - Specifies the minimum value for an input field
pattern * - Specifies a regular expression to check the input value
readonly - Specifies that an input field is read-only (can not be changed)
required * - Specifies that an input field is required (must be filled in)
size - Specifies the width (in characters) of an input field
step * - Specifies the legal number ranges for an input field
value - Specifies the default value for an input field
* = HTML5
This can be seen at: link
These inputs
currently comes with a spin button
to increase and reduce the value, if you want to disable them, you can do with CSS
:
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type=number] {
-moz-appearance:textfield;
}