How to restrict the input field to enter only numbers/digits int and float both. Sometimes we need to allow both integer as well as float value for fields like amount, so in
One-liner solution #1:
Use with the
step
attribute.
// The user can enter up to 4 decimal places.
// Not supported in all browsers, but permits numbers of any decimal precision.
You can also use the min
and/or max
attributes to set the minimum value and/or the maximum value.
One-liner solution #2:
Use a regular text input
element with a RegExp pattern
attribute.
This RegExp accepts numbers beginning with a dot ( .
) and/or a negative sign ( -
).